Python(3.6.4) - 创建随机测验
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python(3.6.4) - 创建随机测验相关的知识,希望对你有一定的参考价值。
#! python3
# randomQuizGenerator.py - Creates quizzes with questions and answers in
# random order, along with the answer key
import random
#the quiz data. Keys are states and values are their capitals.
capitals = {'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': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri': 'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New Mexico': 'Santa Fe', 'New York': 'Albany', '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 City', 'Vermont':'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
#Generate 35 quiz files
for quizNum in range(35):
#Create the quiz and answer the key files.
quizFile = open('capitalsquiz%s.txt' % (quizNum + 1), 'w')
answerKeyFile = open('capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')
#Write out the header for the quiz.
quizFile.write('Name:
Date:
Period:
')
quizFile.write((' ' *20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1))
quizFile.write('
')
#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)]
wrongAnswers = random.sample(wrongAnswers, 3)
answerOptions = wrongAnswers + [correctAnswer]
random.shuffle(answerOptions)
#write the question and the answer options to the quiz file.
quizFile.write('%s. What is the capital of %s?
' % (questionNum + 1,
states[questionNum]))
for i in range(4):
quizFile.write(' %s. %s
' % ('ABCD'[i], answerOptions[i]))
quizFile.write('
')
# Write the answer key to a file.
answerKeyFile.write('%s. %s
' % (questionNum + 1, 'ABCD'[
answerOptions.index(correctAnswer)]))
quizFile.close()
answerKeyFile.close()
当我运行程序时,问题和答案不会生成。我遇到格式化问题了吗?目标是在每个测验中创建35个测验,4个问题和答案。不知何故,出现的唯一事情就是测验的标题而下面没有其他内容。
答案
第二个for循环的缩进有问题请尝试:
#! python3
# randomQuizGenerator.py - Creates quizzes with questions and answers in
# random order, along with the answer key
import random
#the quiz data. Keys are states and values are their capitals.
capitals = {'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': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri': 'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New Mexico': 'Santa Fe', 'New York': 'Albany', '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 City', 'Vermont':'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
#Generate 35 quiz files
for quizNum in range(35):
#Create the quiz and answer the key files.
quizFile = open('capitalsquiz%s.txt' % (quizNum + 1), 'w')
answerKeyFile = open('capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')
#Write out the header for the quiz.
quizFile.write('Name:
Date:
Period:
')
quizFile.write((' ' *20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1))
quizFile.write('
')
#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)]
wrongAnswers = random.sample(wrongAnswers, 3)
answerOptions = wrongAnswers + [correctAnswer]
random.shuffle(answerOptions)
print questionNum,quizFile
# ui
#write the question and the answer options to the quiz file.
quizFile.write('%s. What is the capital of %s?
' % (questionNum + 1,
states[questionNum]))
for i in range(4):
quizFile.write(' %s. %s
' % ('ABCD'[i], answerOptions[i]))
quizFile.write('
')
# Write the answer key to a file.
answerKeyFile.write('%s. %s
' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))
quizFile.close()
answerKeyFile.close()
以上是关于Python(3.6.4) - 创建随机测验的主要内容,如果未能解决你的问题,请参考以下文章