Python Forum
TypeError: not enough arguments for format string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: not enough arguments for format string
#1
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

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:

#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()
Reply
#2
if you use f-strings, the errors will stand out better (not tested):
    # change from
    answerKeyFile.write('%s. %s%\n' %(questionNum +1, 'ABCD'[answerOptions.index(correctAnswer)]))

    # to
    answerKeyFile.write(f"{questionNum +1}. {'ABCD'[answerOptions.index(correctAnswer)]}\n")
Reply
#3
(Jan-09-2022, 07:45 AM)MaartenRo Wrote: answerKeyFile.write('%s. %s%\n' %(questionNum +1, 'ABCD'[answerOptions.index(correctAnswer)]))
You have one percent sign too much.

(Sorry Larz, you were a few seconds ahead of me.)
Reply
#4
Thank you for your input, i deleted the redundant "%" . If i run the script now i get as result:

= RESTART: C:\Users\Maarten\AppData\Local\Programs\Python\Python310-32\randomQuizGenerator.py
It restarts the shell. How can i get the script to make the multiple choice tests?
Reply
#5
(Jan-09-2022, 12:05 PM)MaartenRo Wrote: It restarts the shell. How can i get the script to make the multiple choice tests?
What do you mean? Your script generates files capitalsquiz1.txt through capitalsquiz35.txt and capitalsquiz_answers1.txt through capitalsquiz_answers35.txt. Did you want the program to ask you the questions? Well then you will have to add things. Use print(), input() and if.

And also: Larz60+ was right: you should not use the old-fashoned way to format strings. You had better use f-strings. They are much easyer to understand.
Reply
#6
Thanks. I didn't realize text files were created.
Reply
#7
Did you notice the files remain empty? Can you correct this? Because I want to use your program to train my knowledge of geography. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,232 Jun-09-2023, 06:56 PM
Last Post: kpatil
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,013 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Set string in custom format korenron 4 1,083 Jan-16-2023, 07:46 PM
Last Post: mutantGOD
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,935 Oct-17-2022, 06:29 PM
Last Post: paulo79
  Format String NewPi 2 940 Oct-10-2022, 05:50 PM
Last Post: NewPi
  TypeError: string indices must be integers JonWayn 12 3,383 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,850 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Passing string functions as arguments Clunk_Head 3 1,244 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  string format challenge jfc 2 1,778 Oct-23-2021, 10:30 AM
Last Post: ibreeden
  Print first day of the week as string in date format MyerzzD 2 2,011 Sep-29-2021, 06:43 AM
Last Post: MyerzzD

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020