我正在做一项我需要进行测验的作业。到目前为止,这是我的代码的一部分。
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分。如果你能解释给我,所以我将来不再这样做了。 :)
答案 0 :(得分: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
答案 1 :(得分:0)
评论中的错误表明您正在检查尚未设置的变量。帖子中的匹配行显示为13.在第12行中,您似乎正在设置变量。也许尝试将帖子粘贴回编辑器并重试。发布的代码似乎是在尝试正确访问变量之前设置变量。