I got this question Automate the boring Stuff book. The Code is also given there but When I was Practising this on my own the problem I am getting is this code is not writing in the files after the first for loop.
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:nnDate:nnPeriod:nn')
quizfile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (q + 1))
quizfile.write('nn')
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. %sn" %('ABCD'[i],answer_option[i]))
answerfile.write("%s. %sn" %(q+1,'ABCD'[answer_option.index(correct_answer)]))
quizfile.close()
answerfile.close()
Aucun commentaire:
Enregistrer un commentaire