我的工作中,我需要使用在定義函數中定義的變量,定義環外的任務定義時,代碼:使用變量,裏面一個
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