虽然缩进似乎是一致的,但没有混合的空格/制表符,Python代码会保持返回意外的缩进错误

时间:2015-06-27 19:45:25

标签: python

我的for循环的开头是返回错误。所有缩进都使用制表符完成。我已经检查过,没有空格与标签混合。

修改的 我正在编辑为每个请求包含整个代码,因为看起来错误不在第29行的for循环中,尽管这是错误指向的地方。如前所述,我检查确保所有缩进都是制表符,而不是空格。作为一项额外措施,我使用Sublime的功能将缩进转换为制表符。我也将它转换为4个空格,但我仍然得到同样的错误。

#! 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()

2 个答案:

答案 0 :(得分:1)

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'

现在,显示的行中的问题是您正在尝试编写文字格式字符串:

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

是一个完整的表达式:它写入该字符串,其中包含%个字符,并返回写入的字节数。这意味着该行的其余部分评估为

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

这意味着你有一个int作为左手参数,一个元组作为%的右硬参数。这就是例外情况。

该行应为:

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

请注意'format' % (tuple)表达式全部位于write(...)

答案 1 :(得分:0)

您似乎忘了缩进quizFile.write('\n\n')。或者在它之后添加错误的ident。在它没有意义之后的同一性,该代码不能在上面定义的循环中继续。