宣布早期定义的变量时出现Python错误
我创建了一个菜单,可以显示用户输入的变量。如果用户不输入任何内容,则默认设置。问题是我在执行模块时出错。默认设置的变量:宣布早期定义的变量时出现Python错误,python,Python,我创建了一个菜单,可以显示用户输入的变量。如果用户不输入任何内容,则默认设置。问题是我在执行模块时出错。默认设置的变量:tab=“True”和amount=“N/A” 代码:def菜单():打印(“”*=可选 1. Start the application. 2. Set crate. 3. Set link for crate. * 4. Settings. * """) menu_qsone = str(input("
tab=“True”
和amount=“N/A”
代码:def菜单():打印(“”*=可选
1. Start the application.
2. Set crate.
3. Set link for crate. *
4. Settings. *
""")
menu_qsone = str(input("> "))
if menu_qsone == "4":
clear()
settings()
错误
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
回溯(最近一次呼叫最后一次):
文件“C:\Users\Stagiair\Desktop\PauzeProject\MainFile.py”,第63行,在
菜单()
文件“C:\Users\Stagiair\Desktop\PauzeProject\MainFile.py”,第32行,在菜单中
设置()
文件“C:\Users\Stagiair\Desktop\PauzeProject\MainFile.py”,第36行,在设置中
打印(“1.启动程序时打开选项卡。”+(选项卡))
UnboundLocalError:赋值前引用的局部变量“tab”
您没有在设置
的开头设置选项卡
。在错误指示的行中使用它之前,您需要先进行设置。如果这样做,我是否能够在另一个define函数中调用变量?变量是它们所设置的函数的本地变量。如果您需要在另一个函数中设置的函数内部的值,请在上,return
one函数的值,然后将其作为参数传递给所需的函数。顶部显示“默认设置的变量:tab=“True”和amount=“N/A“”。您在哪里设置它们?编辑;请参阅obove代码我只想指出,一旦代码完成并按预期工作,您可以将代码发布到以请求改进建议。一些事情,例如使用全局变量以及使用“True”
而不是True
,都可以得到改进。
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
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()