为了账号安全,请及时绑定邮箱和手机立即绑定

宣布早期定义的变量时出现 Python 错误

宣布早期定义的变量时出现 Python 错误

MYYA 2023-10-11 22:46:37
我创建了一个菜单,可以在其中显示用户输入的变量。如果用户什么都不输入,则默认设置。问题是我在执行模块时收到错误。默认设置的变量:tab = "True" 和 amount = "N/A"代码:def menu(): print(""" * = Optional    1. Start the application.     2. Set crate.    3. Set link for crate. *    4. Settings. *""")menu_qsone = str(input("> "))if menu_qsone == "4":    clear()    settings()def settings():    print("Settings: ")    print("1. Open a tab when the program is launched." + (tab))    print("2. Times the script will be used." + (amount))    print("3. Return to menu.")    qs_one = str(input("> "))    if qs_one == "1":        clear()        if tab == "True":            tab = "False"            clear()            settings()        if tab == "False":            tab = "True"            clear()            settings()    if qs_one == "2":        clear()        print("How many times would you like to run the script?")        qs_two = str(input("> "))        amount = (qs_two)        clear()        settings()    if qs_one == "3":        clear()        menu()menu()错误;Traceback (most recent call last):  File "C:\Users\Stagiair\Desktop\PauzeProject\MainFile.py", line 63, in <module>    menu()  File "C:\Users\Stagiair\Desktop\PauzeProject\MainFile.py", line 32, in menu    settings()  File "C:\Users\Stagiair\Desktop\PauzeProject\MainFile.py", line 36, in settings    print("1. Open a tab when the program is launched." + (tab))UnboundLocalError: local variable 'tab' referenced before assignment
查看完整描述

1 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

def settings():


    global tab

    global amount

    print("Settings: ")

    print("1. Open a tab when the program is launched." + (tab))

    print("2. Times the script will be used." + (amount))

    print("3. Return to menu.")


    qs_one = str(input("> "))

    if qs_one == "1":

        clear()


        if tab == "True":

            tab = "False"

            clear()

            settings()


        if tab == "False":

            tab = "True"

            clear()

            settings()


    if qs_one == "2":

        clear()

        print("How many times would you like to run the script?")

        qs_two = str(input("> "))

        amount = (qs_two)

        clear()

        settings()


    if qs_one == "3":

        clear()

        menu()


menu()


查看完整回答
反对 回复 2023-10-11
  • 1 回答
  • 0 关注
  • 68 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信