#生成随机试卷及其答案文件import random
#各州及其对应的首府放入capital字典中
capitals ={'Alabama':'Montgomery','Alaska':'Juneau','Arizona':'Phoenix','Arkansas':'Little Rock','California':'Sacramento','Colorado':'Denver','Connecticut':'Hartford','Delaware':'Dover','Florida':'Tallahassee','New Jersey':'Trenton'}for quiz_num inrange(35):# Create the quiz and answer key files.
capitals_quiz_file =open('capitals quiz%s.txt'%(quiz_num+1),'w')
answer_key_file =open('capitals quiz%s answers.txt'%(quiz_num+1),'w')# Write out the header for the quiz.
capitals_quiz_file.write('Name:\n\nData:\n\nPeriod:\n\n')
capitals_quiz_file.write(('State capitals Quiz%s'%(quiz_num+1)).center(50,'*'))
capitals_quiz_file.write('\n\n')# Shuffle the order of the states.
states =list(capitals.keys())
random.shuffle(states)# Loop through all states, making 10 questions for each. for question_num inrange(10):
correct_answer = capitals[states[question_num]]
wrong_answer =list(capitals.values())del wrong_answer[wrong_answer.index(correct_answer)]
wrong_answer = random.sample(wrong_answer,3)
answer_options =[correct_answer]+ wrong_answer
random.shuffle(answer_options)
capitals_quiz_file.write('\n%s.What is the capital of %s?\n\n'%(question_num+1, states[question_num]))for i inrange(4):
capitals_quiz_file.write('ABCD'[i]+'.'+ answer_options[i]+'\n')#create the related answer key files
answer_key_file.write('\n%s.%s'%(question_num+1,'ABCD'[answer_options.index(correct_answer)]))
capitals_quiz_file.close()
answer_key_file.close()