我正在为我的课程创建一个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()
原因是因为“得分”没有被视为全局变量,因此没有在每个问题中都被发现。
在每个函数中,我将在每个问题中使用“全局”函数,如下所示:
score = 0
wrong = 0
def question1():#Ask the first question and calculate score depending on answer
print("Question one! What color is the sky?")
global score
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!")
如果将其添加到每个问题中,它将起作用。
在每个函数中,Python不知道该函数应该在本地(仅在该函数中)使用,还是在各处(全局)使用。
即使您已经在函数外部定义了它,因为尚未在函数中定义它,它也无法将“分数”识别为变量(因此,为什么在定义分数之前就引用了该分数)。
您的代码有些错误。首先是score
不被视为global变量。您需要添加以下行:
global score
到您的问题开始之时。另外,应按以下步骤增加分数:
score += 1
因此该分数不等于正数,而是增加了一个