[英]UnboundLocalError: local variable 'Score' referenced before assignment
[英]“UnboundLocalError: local variable 'score' referenced before assignment”
每次我运行我的程序时,我都会在回答第一个问题后收到该消息。 我不明白为什么会一直发生这种情况,因为我在一切之前都创建了 score 变量,我不明白为什么会出现问题。 我该如何解决这个问题? 这是我的代码:
score = 0
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1():
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
greeting()
quiz1()
变量score
的 scope 不在quiz1
中。 因此,我会将其传入,并根据需要返回:
score = 0
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1(score):
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
return score
greeting()
score = quiz1(score)
当您在 function 定义(方法除外)中引用变量时,python 指的是本地可验证的。 您可以使用global
关键字将score
编辑为全局变量。
score = 0
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1():
global score # add this line #
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
greeting()
quiz1()
make score
global,在 function 的第一行添加global score
。
这可能会有所帮助
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1():
score = 0
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
greeting()
quiz1()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.