变量错误: 未赋值之前引用的 'password'

2024-10-03 15:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

例外情况:

^{pr2}$

如果我写“N”来回答输入,它将运行order(),并且一切正常,除非order()完成后,它会出现错误。我尝试过使password成为全局的,但似乎没有效果。有什么想法吗?在


Tags: toinputifcheckorderpasswordchangeglobal
1条回答
网友
1楼 · 发布于 2024-10-03 15:28:27

如果输入不是"Y",怎么办?那么password就永远不会被创建,因此执行if password == "please"将引发一个错误。也许在你的条件语句之前做password = None。在

相关问题 更多 >