|
| 1 | +import random |
| 2 | + |
| 3 | +# The quiz data. Keys are states and values are their capitals. |
| 4 | + |
| 5 | +capitals = { |
| 6 | + 'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix', |
| 7 | + 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver', |
| 8 | + 'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee', |
| 9 | + 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', |
| 10 | + 'Illinois': 'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', |
| 11 | + 'Kansas': 'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', |
| 12 | + 'Maine': 'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', |
| 13 | + 'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson', |
| 14 | + 'Missouri': 'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', |
| 15 | + 'Nevada': 'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', |
| 16 | + 'New Mexico': 'Santa Fe', 'New York': 'Albany', 'North Carolina': 'Raleigh', |
| 17 | + 'North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City', |
| 18 | + 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence', |
| 19 | + 'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee': 'Nashville', |
| 20 | + 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont': 'Montpelier', |
| 21 | + 'Virginia': 'Richmond', 'Washington': 'Olympia', 'West Virginia': 'Charleston', |
| 22 | + 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne' |
| 23 | +} |
| 24 | + |
| 25 | +# Generate 35 quiz files. |
| 26 | +for quizNum in range(35): |
| 27 | + # Create the quiz and answer key files. |
| 28 | + quizFile = open('capitalsquiz%s.txt' % (quizNum + 1), 'w') |
| 29 | + answerKeyFile = open('capitalsquiz_answers%s.txt' % (quizNum + 1), 'w') |
| 30 | + |
| 31 | + # Write out the header for the quiz. |
| 32 | + quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n') |
| 33 | + quizFile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1)) |
| 34 | + quizFile.write('\n\n') |
| 35 | + |
| 36 | + # Shuffle the order of the states. |
| 37 | + states = list(capitals.keys()) |
| 38 | + random.shuffle(states) |
| 39 | + |
| 40 | +# Loop through all 50 states, making a question for each. |
| 41 | +for questionNum in range(50): |
| 42 | + |
| 43 | + # Get right and wrong answers. |
| 44 | + correctAnswer = capitals[states[questionNum]] |
| 45 | + wrongAnswers = list(capitals.values()) |
| 46 | + del wrongAnswers[wrongAnswers.index(correctAnswer)] |
| 47 | + wrongAnswers = random.sample(wrongAnswers, 3) |
| 48 | + answerOptions = wrongAnswers + [correctAnswer] |
| 49 | + random.shuffle(answerOptions) |
| 50 | + |
| 51 | + # Write the question and the answer options to the quiz file. |
| 52 | + quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1, states[questionNum])) |
| 53 | + |
| 54 | + for i in range(4): |
| 55 | + quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i])) |
| 56 | + quizFile.write('\n') |
| 57 | + |
| 58 | + # Write the answer key to a file. |
| 59 | + answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)])) |
| 60 | +quizFile.close() |
| 61 | +answerKeyFile.close() |
| 62 | + |
0 commit comments