我是一般編程的新手,甚至是 python 的新手。我只有幾天的時間。我正在解決一個我知道很簡單的問題,但我越是修改它,它似乎就越糟糕。
本質上,我要做的是從函式中的條件創建一個變數,我可以將其傳遞給其他函式。我一直在嘗試這樣做,以在世界上最簡單的 RPG 中創建類似的角色創建螢屏。但是,盡管游戲很簡單,但我很快就陷入了困境。我決定保持我所追求的精神,但舉一個更簡單的例子。
現在,我只是想把一頓飯放在一起。
def entree():
options = "1) steak\n2) burger"
print(options)
entree_selection = int(input("Pick one."))
if entree_selection == 1:
entree_choice = "steak"
side_dish()
elif entree_selection == 2:
entree_choice = "burger"
side_dish()
def side_dish():
entree_choice = entree()
print(f"You chose {entree_choice}.")
print("Now choose your side.")
options = "1) baked potato\n2) green beans"
print(options)
side_selection = int(input("Pick one."))
if side_selection == 1:
side_choice = "baked potato"
dessert()
elif side_selection == 2:
side_choice = "green beans"
dessert()
def dessert():
entree_choice = entree()
side_choice = side_dish()
print(f"So far we have {entree_choice} and {side_choice}.")
print("How about dessert?")
options = "1) cake\n2) ice cream"
print(options)
dessert_selection = int(input("Pick one."))
if side_selection == 1:
dessert_choice = "cake"
your_meal()
elif side_selection == 2:
dessert_choice = "ice cream"
your_meal()
def your_meal():
entree_choice = entree()
side_choice = side_dish()
dessert_choice = dessert()
print(
f'You will be having {entree_choice} with {side_choice} and {dessert_choice}')
entree()
side_dish()
dessert()
your_meal()
對我來說,問題是函式 a一遍又一遍地重復,而沒有運行函式 b
To be honest, I've lost track of all the things I have tried. I've tried at least 10 things from YouTube and at least the same number from here.
uj5u.com熱心網友回復:
功能A重復,因為在函式A結束時,您呼叫函式b,函式b的開頭,您呼叫函式a。重新啟動回圈。
函式b始終運行,它無法完成,因為您已請求函式b始終重新運行函式a。
為了不重復,您無法要求side_dish重新運行entree。
def side_dish():
entree_choice = entree() # this is the problem
print(f"You chose {entree_choice}.")
print("Now choose your side.")
相反,將entree_choice變數傳遞給您的side_dish。
def side_dish(entree_choice):
print(f"You chose {entree_choice}.")
print("Now choose your side.")
要繼續實作示例,您將創建一個令人垂詢的函式,它占用了先前的選擇。
def dessert(entree_choice, side_choice):
print(f"So far we have {entree_choice} and {side_choice}.")
print("How about dessert?")
自然而然地讓我們進入最終功能:
def your_meal(entree_choice, side_choice, dessert_choice):
print(
f'You will be having {entree_choice} with {side_choice} and {dessert_choice}')
隨著所有這些都說,我建議略有不同的實施。基本上,有一個“控制器”。這個東西在一個單一級別控制所有邏輯。
entree_choice = entree()
side_choice = side_dish()
dessert_choice = dessert()
print(f'You will be having {entree_choice} with {side_choice} and {dessert_choice}')
為了完成這一點,兩件事:
- Modify your underlying
entreefunction to not ask about the side choice. Instead let the controller ask. - Return the entree that they chose.
def entree():
options = "1) steak\n2) burger"
print(options)
entree_selection = int(input("Pick one."))
if entree_selection == 1:
entree_choice = "steak"
return entree_choice
elif entree_selection == 2:
entree_choice = "burger"
return entree_choice
uj5u.com熱心網友回復:
您缺少的是函式“回傳值”的概念。首先將您的任務分解為離散的單元。使用您的示例your_meal,程式將entree()首先運行該函式并將該函式的結果設定為變數 entree_choice。但要使其作業,該函式entree需要具有一個(或多個)return 'steak'或類似的型別陳述句。這就是程式的不同部分相互“通信”的方式:通過引數和函式的回傳值。
另請注意,如果您呼叫 entree,然后 entree 然后呼叫 side_dish 和 side_dish 然后呼叫 entree,那么程式將永遠不會停止回圈(這是您所描述的問題)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/452191.html
標籤:python function variables conditional-statements parameter-passing
