我有这个问题自动化无聊的东西书。代码也在那里给出,但当我自己练习这个问题时,我得到的问题是这个代码在第一个for循环之后没有写入文件。
import random
import os
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'}
#print (capitals)
for q in range(35):
quizfile = open("capitalquiz%s.txt" %(q + 1), 'w')
answerfile = open("Answerfile%s.txt"%(q + 1),'w')
quizfile.write('Name:\n\nDate:\n\nPeriod:\n\n')
quizfile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (q + 1))
quizfile.write('\n\n')
states = list(capitals.keys())
random.shuffle(states)
quizfile.write("wefkjqennqkeanfeqkjn")
for i in range(50):
correct_answer = capitals[states[i]]
wrong_answer = list(capitals.values())
del wrong_answer[correct_answer.index(correct_answer)]
wrong_answers = random.sample(wrong_answer,3)
answer_option = wrong_answers + [correct_answer]
random.shuffle(answer_option)
for item in range(50):
#quizfile.write("******************************************************")
quizfile.write("%s.what is the capital of %s" %(item+1,states[item]))
for i in range(4):
quizfile.write("%s. %s\n" %('ABCD'[i],answer_option[i]))
answerfile.write("%s. %s\n" %(q+1,'ABCD'[answer_option.index(correct_answer)]))
quizfile.close()
answerfile.close()
答案 0 :(得分:0)
你有一些需要嵌套在其他环路中的for循环。您现在尝试访问范围外的变量。
您的代码已清理完毕:
import random
import os
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'}
for q in range(35):
quizfile = open("capitalquiz%s.txt" %(q + 1), 'w')
answerfile = open("Answerfile%s.txt"%(q + 1),'w')
quizfile.write('Name:\n\nDate:\n\nPeriod:\n\n')
quizfile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (q + 1))
quizfile.write('\n\n')
states = list(capitals.keys())
random.shuffle(states)
for i in range(50):
correct_answer = capitals[states[i]]
wrong_answer = list(capitals.values())
del wrong_answer[correct_answer.index(correct_answer)]
wrong_answers = random.sample(wrong_answer,3)
answer_option = wrong_answers + [correct_answer]
random.shuffle(answer_option)
quizfile.write("%s.what is the capital of %s \n" %(i+1,states[i]))
for j in range(4):
quizfile.write("%s. %s\n" %('ABCD'[j],answer_option[j]))
quizfile.write('\n')
answerfile.write("%s. %s\n" %(i+1,'ABCD'[answer_option.index(correct_answer)]))
quizfile.close()
answerfile.close()
它不是最漂亮但我觉得它会做你想做的事。