Jan-09-2022, 07:45 AM
(This post was last modified: Jan-09-2022, 09:16 AM by Larz60+.
Edit Reason: fixed error tags
)
I am doing an assignement program from a book. I need to write a program that
makes a test with 50 mutliple choice questions asking what the capital is of each state. Somehow i get
This is the script that i created:
makes a test with 50 mutliple choice questions asking what the capital is of each state. Somehow i get
Error:Traceback (most recent call last):
File "C:\Users\Maarten\AppData\Local\Programs\Python\Python310-32\randomQuizGenerator.py", line 57, in <module>
answerKeyFile.write('%s. %s%\n' %(questionNum +1, 'ABCD'[answerOptions.index(correctAnswer)]))
TypeError: not enough arguments for format string
What am i doing wrong? Any input is much appreciated!This is the script that i created:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#randomQuizGenerator.py import random 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 ): #TODO: create the quiz and answer key files. quizFile = open ( 'capitalsquiz%s.txt' % (quizNum + 1 ), 'w' ) answerKeyFile = open ( 'capitalsquiz_answers%s.txt' % (quizNum + 1 ), 'w' ) #TODO: write out the header for the quiz. quizFile.write( 'Name:\n\nDate:\n\nPeriod:\n\n' ) quizFile.write(( ' ' * 20 ) + 'State Capitals Quiz (Form %s)' % (quizNum + 1 )) quizFile.write( '\n\n' ) #TODO: shuffle the order of states. states = list (capitals.keys()) random.shuffle(states) #TODO: 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 answer options to the quiz file. quizFile.write( '%s. What is the capital of %s?\n' % (questionNum + 1 ,states[questionNum])) for i in range ( 4 ): quizFile.write( ' %s.%s\n' % ( 'ABCD' [i], answerOptions[i])) quizFile.write( '\n' ) #Write the answer key to a file. answerKeyFile.write( '%s. %s%\n' % (questionNum + 1 , 'ABCD' [answerOptions.index(correctAnswer)])) quizFile.close() answerKeyFile.close() |