UnboundLocalError:局部变量'密码'在分配之前引用

时间:2014-06-04 11:18:36

标签: python python-3.x local

def start():

    global smallq
    global medq
    global larq
    check = input("Would you like to change prices? Press Y to change or Press N to  continue to order: ")

    if check == "Y":
        password = input("What is the password?: ")
    else:
        print("Continuing to order...")
        smallq = 50
        medq = 120
        larq = 180
        order()

    if password == "please":
        quilt_price()
    else:
        print("Wrong password", "\n", "Continuing to order...")
        smallq = 50
        medq = 120
        larq = 180
        order()

例外:

Traceback (most recent call last):
  File "/Users/palerj09/Documents/SAC RW.py", line 128, in <module>
    start()
  File "/Users/palerj09/Documents/SAC RW.py", line 18, in start
    if password == "please":
UnboundLocalError: local variable 'password' referenced before assignment

如果我写&#34; N&#34;回答它运行的输入order()并且一切正常,除了order()完成后它会出现错误。我试过让password全球化,但这似乎并没有起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果输入不是"Y"怎么办?然后永远不会创建password,因此if password == "please"会引发错误。也许在条件陈述之前password = None