[英]“UnboundLocalError: local variable 'score' referenced before assignment”
Every time I run my program I get that message after answering the first question.每次我运行我的程序时,我都会在回答第一个问题后收到该消息。 I don't understand why this keeps happening because I have created the score variable before everything, I don't see why there is an issue.
我不明白为什么会一直发生这种情况,因为我在一切之前都创建了 score 变量,我不明白为什么会出现问题。 How do I solve this?
我该如何解决这个问题? This is my code:
这是我的代码:
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()
The scope of the variable score
is not in quiz1
.变量
score
的 scope 不在quiz1
中。 Therefore, I would pass it in, and return it as fit:因此,我会将其传入,并根据需要返回:
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)
When you refer to a variable in a function definition (other than methods) python refers to the local veriable.当您在 function 定义(方法除外)中引用变量时,python 指的是本地可验证的。 You can edit
score
as a global variable with the global
keyword.您可以使用
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, add global score
in the first line in the function. make
score
global,在 function 的第一行添加global score
。
This might help这可能会有所帮助
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.