我是一般編程的新手,甚至是 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/qita/454226.html
標籤:python function variables conditional-statements parameter-passing
上一篇:具有先前創建的變數的串列中的問題。賦值前參考的錯誤區域變數“Victory”
下一篇:如何避免多載方法的多重定義
