-1

我正在做一项需要进行测验的作业。到目前为止,这是我的代码的一部分。

answer = input("Your answer: ")
guessesTaken = 0
points = 0
if answer == 'Call Of Duty' or answer == 'Call of duty' or answer == 'Call of duty' or answer == 'a)' or answer == 'call of duty' or answer == 'a':
    print("You are correct!")
    points = points + 2
    print("You scored two points!")
else: 
    guessesTaken = guessesTaken + 1
    print("Incorrect!")
    print("You have", guessesTaken, "guess remaining!")
answerb = input("Your answer: ")
if answerb == 'Call Of Duty' or answerb == 'Call of duty' or answerb == 'Call of duty' or answerb == 'a)' or answerb == 'call of duty' or answerb == 'a':
    points = points + 1
    print("You scored one point!")

如果 answerb 出现错误,我会收到错误消息。我无法弄清楚为什么会收到此错误。这部分代码旨在为您答对的每个问题评分。计分制度如下,第一次答对2分,第二次答对1分,第三次答错0分。我认为使用 answerb 会让我再试一次并给 1 分。如果你能解释一下,我以后不会再这样做了。:)

4

2 回答 2

2

就像 minitech 建议的那样,你可能只是缩进answerb = input(...)太远了。我稍微修改了你的代码:

maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
while guessesTaken<maxGuesses:
    answer = input("Your answer: ")
    if answer.lower() == 'call of duty' or answer.lower() == 'a' or answer.lower() == 'a)':
        print("You are correct!")
        points = points + maxGuesses-guessesTaken
        print("You scored %d points!" %(maxGuesses-guessesTaken))
        break
    else:
        print("Incorrect")
        print("You have %d guesses remaining!" %(maxGuesses-guessesTaken-1))
        guessesTaken += 1
于 2013-10-28T02:09:02.593 回答
0

您评论中的错误表明第 28 行您正在检查尚未设置的变量。您帖子中的匹配行似乎是 13。在第 12 行中,您似乎正在设置变量。也许尝试将您的帖子粘贴回您的编辑器并重试。发布的代码似乎在尝试正确访问它之前设置了变量。

于 2013-10-28T02:33:50.907 回答