[英]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.