简单的选择游戏Python IDLE 3.4如何继续不同的选择?

问题描述:

我很新的节目,并做一个简单的选择游戏:简单的选择游戏Python IDLE 3.4如何继续不同的选择?

Answer = (input("You meet a bear, what do you do? A) Give the bear a hug B) Run away")) 
if Answer == ("A)"): 
     print("The bear chopped your hand off!") 
else: 
     print("Good choice, but the bear is running after you") 

但是我怎么去?就像一个切碎的手或通过森林(2个选择至少两个以前的成果)

+1

你可以,但一个if/else语句内的if/else语句多次你要。只需添加另一级别的缩进。 –

+0

听起来你正在寻找一个为你的游戏寻找决策树架构。有很多很好的资源讨论如何实现这一点。*将是一个好的开始。 https://en.wikipedia.org/wiki/Decision_tree – rfj001

这是一个开始,你可以希望找出如何在:)

def main(): 
    print("Tristan Aljamaa's Simple Python Choice Game") 
    print("===========================================") 
    print("Instructions: Type A) or B) etc. whenever prompted\n") 
    game() 

def game(): 
    Answer = (input("You meet a bear, what do you do?\n A) Give the bear a hug\n B) Run away \nEnter A) or B):")) 

    if Answer == ("A)"): 
    print("The bear chopped your hand off!") 
    player_died() 
    else: 
    print("Good choice, but the bear is running after you") 
    player_ran() 

def player_died(): 
    print("You died, the bear eventually ate you...") 
    Answer = (input("Game Over!\n\nEnter N) for New Game:")) 
    if Answer == ("N)"): 
    main() 
    else: 
    print("Good Bye!") 

def player_ran(): 
    Answer = (input("You find an exit from the forest, what do you do\n A) Exit forest\n B) Run around in forest \nEnter A) or B):")) 
    if Answer == ("A)"): 
    print("You exited the forest") 
    player_crossedRoad() 
    else: 
    print("You (although insanly) chose to run around in the forest") 
    player_stillRunsinForest() 

def player_crossedRoad(): 
    print("You get the idea...") 

main() #for testing on this online editor use the below line when calling the .py file on your computer 
if __name__ == "__main__":main() 

尽量扩大游戏出来here

+0

现在的“其他人”,“player_crossedRoad”后somewhy无效的语法?这就是我所做的:高清player_ran(): 答案=(输入(“您可以找到林的退出,你是做什么\ n A)退出森林\ n B)在森林奔波\ n输入A)或B) : “)) 如果答案==(” A) “): 打印(” 你离开森林 “) player_crossedRoad() 其他: 打印(” 你(虽然insanly)选择在森林里乱跑“) player_stillRunsinForest if __name__ ==”__main __“:main() –

+0

你的问题是什么? – shash678

+0

完全随机无效的语法,看起来和其他语法一样,后面有一个冒号。 –

您可以创建不同情况不同功能/程序运行在前进后添加一个选项。例如:

def choppedHand(): 
    selection = input("The bear chopped your hand off! What do you do now? \n 1.Fight the bear with one hand.\n 2. Scream and search for help.\n 3. Cry and beg for mercy") 
    if selection == "1": 
     fight() 
    elif selection == "2": 
     scream() 
    else: 
     cry() 

def run(): 
    selection = input ("Good choice, but the bear is running after you. What do you do now? 1.Run through the trees. 2.Run in the plain") 
    #etc etc, same as top. 

Answer = (input("You meet a bear, what do you do?\n 1.Give the bear a hug.\n 2.Run away.")) 
if Answer == ("1"): 
    choppedHand() 
else: 
    run() 

这仅仅是一个例子,但使用功能,您可以创建不同的情况不同的选项,并在你的代码的其他部分调用一个函数。例如,你的角色会在不同的情况下失去他的胳膊,在这种情况下,你只需要回想一下你的功能choppedHand()。

我希望这是你所期待的。