我正在为我的班级制作一个python问答游戏。然而,我很难接受错误的答案,这给我带来了一个“UnboundLocalError:赋值前引用的局部变量‘score’”错误。还有一个事实是,分数不会累积,也不会在比赛结束时累积到总分数。我担心一旦我发现让我的程序重播这些问题只会使事情进一步复杂化。我研究了其他一些线程,并尝试了将“score=+1”更改为另一个变量(如score=score+1)的方法,但根据我的经验水平,我无法理解这一点
ques1=['A. Blue', 'B. Red', 'C. Green', 'D. Yellow']#answers for question 1
ques2=['A. Solvent', 'B. Saltwater', 'C. Saliva', 'D. Syrup']#answers for question 2
ques3=['A. 2', 'B. 12', 'C. 21', 'D. 100']#answers for question 3
ques4=['A. Bryan Cranston', 'B. Chris Evans', 'C. Snoop Dogg', 'D. Arnold Schwarzenegger']#answers for question 4
ques5=['A. False', 'B. Apple', 'C. The Sky', 'D. Saltwater']#answers for question 5
score = 0
wrong = 0
def question1():#Ask the first question and calculate score depending on answer
print("Question one! What color is the sky?")
for ques in ques1:
print(ques)
ans1=input("")
if ans1 == "A":
score=+1
print("Correct answer! You get one point! You currently have a score of ",score," !")
elif ans1 == "B":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans1 == "C":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans1 == "D":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
else:#Force them to answer until something valid is entered
print("Input something valid! A, B, C, or D!")
def question2():#Ask the second question and calculate score depending on answer
print("Question Two! What liquid is the ocean made of?")
for ques in ques2:
print(ques)
ans2=input("")
if ans2 == "B":
score=+1
print("Correct answer! You get one point! You currently have a score of ",score," !")
elif ans2 == "A":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans2 == "C":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans2 == "D":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
else:#Force them to answer until something valid is entered
print("Input something valid! A, B, C, or D!")
def question3():#Ask the third question and calculate score depending on answer
print("Question Three! What is the legal drinking age in Ohio?")
for ques in ques3:
print(ques)
ans3=input("")
if ans3 == "C":
score=+1
print("Correct answer! You get one point! You currently have a score of ",score," !")
elif ans3 == "A":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans3 == "B":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans3 == "D":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
else:#Force them to answer until something valid is entered
print("Input something valid! A, B, C, or D!")
def question4():#Ask the fourth question and calculate score depending on answer
print("Question Four! Who played the teminator in the movie Terminator?")
for ques in ques4:
print(ques)
ans4=input("")
if ans4 == "D":
score=+1
print("Correct answer! You get one point! You currently have a score of ",score," !")
elif ans4 == "A":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans4 == "C":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans4 == "B":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
else:#Force them to answer until something valid is entered
print("Input something valid! A, B, C, or D!")
def question5():#Ask the fifth question and calculate score depending on answer
print("Question Five! What is the opposite of true?")
for ques in ques5:
print(ques)
ans5=input("")
if ans5 == "A":
score=+1
print("Correct answer! You get one point! You currently have a score of ",score," !")
elif ans5 == "B":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans5 == "C":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
elif ans5 == "D":
wrong=+1
print("Uh oh, wrong answer! You currently have a score of ",score," !")
else:#Force them to answer until something valid is entered
print("Input something valid! A, B, C, or D!")
def scoretotal():#Display the score
print("In the end you got a total of ",score," correct and ",wrong, " wrong.")
def playagain():#Repeats all previous questions if the user wants, continuing to build score up
print("Want to play again without fully restarting? I don't know how to let you!")
def main():#Assemble program steps
print("Time to play a trivia game! You will be asked a list of questions in order! Answer with an uppercase A, B, C, or D.")
question1()
question2()
question3()
question4()
question5()
scoretotal()
#playagain()
#activate program
main()
“UnboundLocalError:分配前引用的局部变量“score” 当您尝试访问在函数之前在外部定义的变量时,会发生这种情况。 前
n = 0 def func(): return n + 1
要避免这种情况,您可以执行以下操作:
def func(n): return n + 1 n = 0 func(n)
或
def func(): return n + 1 gobal n n = 0
您的代码有一些问题。第一个是
score
不被视为global变量。您需要添加以下行:到问题的开头。此外,递增分数应如下所示:
所以分数不等于正数,而是增加1
原因是
score
没有被视为一个全局变量,因此没有在每个问题中被提及我会在每个问题中使用
global
函数,如下所示:如果你在每个问题上都加上这个,它就会起作用
在每个函数中,Python都不知道变量应该在本地(仅在该函数中)还是在任何地方(全局)使用,因此它默认为仅在本地使用
即使您在函数外部定义了它,因为它没有在函数内部定义,它也不会将
score
识别为变量(因此它说在定义之前正在引用分数)最后,当执行增广赋值时,运算符位于等号之前。因此
score
的定义如下:相关问题 更多 >
编程相关推荐