Advertisement
mengyuxin

meng.randomQuizGenerator.py

Jan 2nd, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. #! python3
  2. # randomQuizGenerator.py 生成随机的测试试卷文件
  3. # Creates quizzes with questions and answers in random order, along with the answer key.
  4.  
  5. import random
  6. import pprint
  7.  
  8. # The quiz data. keys are states and values are their capitals.
  9. capitals = {
  10.     'Alabama': 'Montgomery',
  11.     'Alaska': 'Juneau',
  12.     'Alabama': 'Montgomery  ',
  13.     'Alaska': 'Juneau  ',
  14.     'Arizona': 'Phoenix  ',
  15.     'Arkansas': 'Little Rock',
  16.     'California': 'Sacramento',
  17.     'Colorado  ': 'Denver',
  18.     'Connecticut  ': 'Hartford',
  19.     'Delaware': 'Dover ',
  20.     'Florida': 'Tallahassee  ',
  21.     'Georgia': 'Atlanta  ',
  22.     'Hawaii': 'Honolulu  ',
  23.     'Idaho': 'Boise  ',
  24.     'Illinois': 'Springfield  ',
  25.     'Indiana': 'Indianapolis  ',
  26.     'Iowa': 'Des Moines ',
  27.     'Kansas': 'Topeka  ',
  28.     'Kentucky': 'Frankfort  ',
  29.     'Louisiana': 'Baton Rouge ',
  30.     'Maine': 'Augusta  ',
  31.     'Maryland': 'Annapolis  ',
  32.     'Massachusetts': 'Boston  ',
  33.     'Michigan': 'Lansing  ',
  34.     'Minnesota': 'St. Paul ',
  35.     'Mississippi': 'Jackson  ',
  36.     'Missouri': 'Jefferson City ',
  37.     'Montana': 'Helena  ',
  38.     'Nebraska': 'Lincoln  ',
  39.     'Nevada': 'Carson City ',
  40.     'New Hampshire': 'Concord ',
  41.     'New Jersey': 'Trenton ',
  42.     'New Mexico': 'Santa ',
  43.     'New York': 'Albany Fe',
  44.     'North Carolina': 'Raleigh ',
  45.     'North Dakota': 'Bismarck ',
  46.     'Ohio': 'Columbus  ',
  47.     'Oklahoma': 'Oklahoma City ',
  48.     'Oregon': 'Salem  ',
  49.     'Pennsylvania': 'Harrisburg  ',
  50.     'Rhode Island': 'Providence ',
  51.     'South Carolina': 'Columbia ',
  52.     'South Dakota': 'Pierre ',
  53.     'Tennessee': 'Nashville  ',
  54.     'Texas': 'Austin  ',
  55.     'Utah': 'Salt Lake ',
  56.     'Vermont': 'Montpelier  City',
  57.     'Virginia': 'Richmond  ',
  58.     'Washington': 'Olympia  ',
  59.     'West Virginia': ' Charleston ',
  60.     'Wisconsin': 'Madison  ',
  61.     'Wyoming': 'Cheyenne  '}
  62.  
  63. #pprint.pprint(capitals)
  64.  
  65. # Generate 35 quiz files.
  66. for quizNum in range(35):
  67.     # Create the quiz and answer key files.
  68.     quizFile = open('.\\#temp\\capitalsquiz%s.txt' % (quizNum + 1), 'w')
  69.     answerKeyFile = open('.\\#temp\\capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')
  70.  
  71.     #Write out the header for the quiz.
  72.     quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n')
  73.     quizFile.write((' ' * 20) + 'State Capitals Quiz (form %s)' % (quizNum + 1))
  74.     quizFile.write('\n\n')
  75.  
  76.     # Shuffle the order of the states.
  77.     states = list(capitals.keys())
  78.     random.shuffle(states)
  79.  
  80.     # Loop through all 50 states, making a question for each.
  81.     for questionNum in range(50):
  82.         # Get right and wrong answers.
  83.         correctAnswer = capitals[states[questionNum]]
  84.         wrongAnswers = list(capitals.values())
  85.         del wrongAnswers[wrongAnswers.index(correctAnswer)]
  86.         wrongAnswer = random.sample(wrongAnswers, 3)
  87.         answerOptions = wrongAnswer + [correctAnswer]
  88.         random.shuffle(answerOptions)
  89.  
  90.         # Write the question and answer options to the quiz file.
  91.         quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))
  92.         for i in range(4):
  93.             quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i]))
  94.         quizFile.write('\n')
  95.  
  96.         # Write the answer key to a file.
  97.         answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))
  98.        
  99.     quizFile.close()
  100.     answerKeyFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement