2016-04-02 86 views
1

我是python的新手,我正在自己做一個簡單的遊戲來幫助自己學習它。我已經到了一個地步,我想在那裏做出一個決定,影響它之後的所有行動。在Python代碼中導航

print ("Left down the hallway? Or right?") 

action4 = input()  
if action4 == ("left"): 
    print ("You turn left and proceed down this hallway") 
elif action4 == ("right"): 
    print ("You turn right and proceed down this hallway") 

#The game will branch off from this 

我可以列出兩個不同版本的代碼分支。一個你選擇去的地方,另一個你選擇去的地方。我希望每個方向都完全不同。我會如何去做這件事?提前致謝!

+0

爲什麼不根據調用單獨遊戲版本的if條件調用不同的函數? – Pythonista

+0

我不太明白你的意思,對不起。你能詳細說明一下嗎?我只是一個新手:) –

回答

0

考慮使用兩種功能來完成這項工作。它看起來像這樣:

# define your functions first 

def function_left(): 
    # do something here 

def function_right(): 
    # do something here 

#then call your functions from the if/elif block 

action4 = input("Left down the hallway? Or right?")  
if action4 == ("left"): 
    print ("You turn left and proceed down this hallway") 
    function_left() 
elif action4 == ("right"): 
    print ("You turn right and proceed down this hallway") 
    function_right() 
+0

這些功能是否需要包含每個遊戲版本的所有代碼? –

+0

不是,您可以定義其他功能。基本上,如果你有一個任務你會執行多次,你應該定義一個函數來執行這個任務。那有意義嗎? – MarkyPython