简体   繁体   English

“UnboundLocalError:赋值前引用了局部变量‘score’”

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

相关问题 UnboundLocalError:赋值前引用了局部变量“Score” - UnboundLocalError: local variable 'Score' referenced before assignment Python 错误:UnboundLocalError:赋值前引用了局部变量“score1” - Python error: UnboundLocalError: local variable 'score1' referenced before assignment UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM