Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- # randomQuizGenerator.py 生成随机的测试试卷文件
- # Creates quizzes with questions and answers in random order, along with the answer key.
- import random
- import pprint
- # The quiz data. keys are states and values are their capitals.
- capitals = {
- 'Alabama': 'Montgomery',
- 'Alaska': 'Juneau',
- 'Alabama': 'Montgomery ',
- 'Alaska': 'Juneau ',
- 'Arizona': 'Phoenix ',
- 'Arkansas': 'Little Rock',
- 'California': 'Sacramento',
- 'Colorado ': 'Denver',
- 'Connecticut ': 'Hartford',
- 'Delaware': 'Dover ',
- 'Florida': 'Tallahassee ',
- 'Georgia': 'Atlanta ',
- 'Hawaii': 'Honolulu ',
- 'Idaho': 'Boise ',
- 'Illinois': 'Springfield ',
- 'Indiana': 'Indianapolis ',
- 'Iowa': 'Des Moines ',
- 'Kansas': 'Topeka ',
- 'Kentucky': 'Frankfort ',
- 'Louisiana': 'Baton Rouge ',
- 'Maine': 'Augusta ',
- 'Maryland': 'Annapolis ',
- 'Massachusetts': 'Boston ',
- 'Michigan': 'Lansing ',
- 'Minnesota': 'St. Paul ',
- 'Mississippi': 'Jackson ',
- 'Missouri': 'Jefferson City ',
- 'Montana': 'Helena ',
- 'Nebraska': 'Lincoln ',
- 'Nevada': 'Carson City ',
- 'New Hampshire': 'Concord ',
- 'New Jersey': 'Trenton ',
- 'New Mexico': 'Santa ',
- 'New York': 'Albany Fe',
- 'North Carolina': 'Raleigh ',
- 'North Dakota': 'Bismarck ',
- 'Ohio': 'Columbus ',
- 'Oklahoma': 'Oklahoma City ',
- 'Oregon': 'Salem ',
- 'Pennsylvania': 'Harrisburg ',
- 'Rhode Island': 'Providence ',
- 'South Carolina': 'Columbia ',
- 'South Dakota': 'Pierre ',
- 'Tennessee': 'Nashville ',
- 'Texas': 'Austin ',
- 'Utah': 'Salt Lake ',
- 'Vermont': 'Montpelier City',
- 'Virginia': 'Richmond ',
- 'Washington': 'Olympia ',
- 'West Virginia': ' Charleston ',
- 'Wisconsin': 'Madison ',
- 'Wyoming': 'Cheyenne '}
- #pprint.pprint(capitals)
- # Generate 35 quiz files.
- for quizNum in range(35):
- # Create the quiz and answer key files.
- quizFile = open('.\\#temp\\capitalsquiz%s.txt' % (quizNum + 1), 'w')
- answerKeyFile = open('.\\#temp\\capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')
- #Write out the header for the quiz.
- quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n')
- quizFile.write((' ' * 20) + 'State Capitals Quiz (form %s)' % (quizNum + 1))
- quizFile.write('\n\n')
- # Shuffle the order of the states.
- states = list(capitals.keys())
- random.shuffle(states)
- # Loop through all 50 states, making a question for each.
- for questionNum in range(50):
- # Get right and wrong answers.
- correctAnswer = capitals[states[questionNum]]
- wrongAnswers = list(capitals.values())
- del wrongAnswers[wrongAnswers.index(correctAnswer)]
- wrongAnswer = random.sample(wrongAnswers, 3)
- answerOptions = wrongAnswer + [correctAnswer]
- random.shuffle(answerOptions)
- # Write the question and answer options to the quiz file.
- quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))
- for i in range(4):
- quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i]))
- quizFile.write('\n')
- # Write the answer key to a file.
- answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))
- quizFile.close()
- answerKeyFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement