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

我创建了一个菜单,可以在其中显示用户输入的变量。如果用户什么都不输入,则默认设置。问题是我在执行模块时收到错误。默认设置的变量: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


MYYA
浏览 72回答 1
1回答

慕桂英4014372

def settings():&nbsp; &nbsp; global tab&nbsp; &nbsp; global amount&nbsp; &nbsp; print("Settings: ")&nbsp; &nbsp; print("1. Open a tab when the program is launched." + (tab))&nbsp; &nbsp; print("2. Times the script will be used." + (amount))&nbsp; &nbsp; print("3. Return to menu.")&nbsp; &nbsp; qs_one = str(input("> "))&nbsp; &nbsp; if qs_one == "1":&nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; if tab == "True":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tab = "False"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; settings()&nbsp; &nbsp; &nbsp; &nbsp; if tab == "False":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tab = "True"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; settings()&nbsp; &nbsp; if qs_one == "2":&nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; print("How many times would you like to run the script?")&nbsp; &nbsp; &nbsp; &nbsp; qs_two = str(input("> "))&nbsp; &nbsp; &nbsp; &nbsp; amount = (qs_two)&nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; settings()&nbsp; &nbsp; if qs_one == "3":&nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; menu()menu()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python