Sam
Sam

Reputation: 71

Python code keeps return unexpected indent error although indentation appears to be consistent, no mixed spaces/tabs

The beginning of my for loop is returning the error. All indentation has been done with tabs. I've checked and there are no spaces mixed with tabs.

EDIT I'm editing to include the entire code, per request, because it looks like the error isn't in the for loop on line 29, although that's where the error is point to. As mentioned before, I've check to make sure all indentation is tabs, not mixed with space. As an extra measure, I used Sublime's feature to convert indentation to tabs. I also converted it to 4 spaces, but am still getting the same error.

#! python3
#randomQuizGenerator.py - Creates quizzes with questions and answers in 
#random order, 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 key files
    quizFile = open('capitalsquiz%s.text' % (quizNum + 1), 'w')
    answerKeyFile = open('capitalsquiz_capitalsquiz_answers%s' % (quizNum + 1), 'w')

    #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')

    #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 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, 'ACBD'[answerOptions.index(correctAnswer)]))
    quizFile.close()
    answerKeyFile.close()

Upvotes: 0

Views: 273

Answers (2)

Useless
Useless

Reputation: 67733

Traceback (most recent call last):
  File "../indent.py", line 40, in <module>
    quizFile.write('%s. What is the capital of %s?\n') % (questionNum + 1, states[questionNum])
TypeError: unsupported operand type(s) for %: 'int' and 'tuple'

Now, the problem in the line shown is that you're trying to write the literal format string:

quizFile.write('%s. What is the capital of %s?\n')

is a complete expression: it writes that string, with the % characters in it, and returns the number of bytes written. This means the rest of the line evaluates as

(bytes written)  % (questionNum + 1, states[questionNum])

which means you have an int as the left-hand argument, and a tuple as the right-hard argument to %. This is what the exception says.

The line should be:

quizFile.write('%d. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))

note the 'format' % (tuple) expression is all inside the write(...).

Upvotes: 1

Mike
Mike

Reputation: 768

Looks like you forgot to indent your quizFile.write('\n\n'). Or added wrong idents after it. Identation after it have no meaning, this code cannot be continued in a loop defined above.

Upvotes: 0

Related Questions