亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

宣布早期定義的變量時出現 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 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號