我的工作中,我需要使用在定义函数中定义的变量,定义环外的任务定义时,代码:使用变量,里面一个
def startmenu(): #this is to call back here at any time
startmenuoption = 1
while startmenuoption == 1:
startoption = input("Would you like to create, check or quit?")
if startoption in ["Check", "check"]:
print("You chose check!")
startmenuoption = 0
elif startoption in ["Create", "create"]:
print("You chose create!")
startmenuoption = 0
elif startoption in ["Quit", "quit"]:
print("You quit!")
startmenuoption = 0
else:
print("Invalid reason try again!")
startmenu()
if startoption in ["Check"]:
print("Checking!")
else:
print("Okay!")
我知道它似乎是一个简单的选项来删除定义循环,但这正是我想要避免的,因为它是我所需要的。
要做到这一点的方法是通过传递和返回函数中的值 –
函数调用下的代码的目的是什么?它看起来像在函数本身的功能 – Mangohero1
'return startoption'中执行的功能一样。然后,在调用你的函数时,就像'startoption = startmenu()'一样。这些是你所拥有的最低(不是最好的)改变。 – CristiFati