简单选择游戏 Python IDLE 3.4 如何进行不同的选择?

Simple choice game Python IDLE 3.4 How to proceed with different choices?

我是编程新手,正在做一个简单的选择游戏:

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 个选择)

您可以针对不同的情况创建不同的 functions/procedures。例如:

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()。

希望这就是您要找的。

这是一个开始,希望您能弄清楚如何扩展 :)

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

def invalid():
    print("Answer not valid")
def game():
    Answer = input("You meet a bear, what do you do?\nA) Give the bear a hug\nB) Run away\n>? ")
    if Answer == ("A"):
           print("The bear chopped your hand off!")
           Answer2 = input("The bear is still around you, what will you do?\nA) Bleed out and accept your fate\nB) Try to fight back\n>? ")
           if Answer2 == ("A"):
               print("You bled out and died")
           elif Answer2 == ("B"):
               print("You attempt to fight back\nYou have failed, and died")
    elif Answer == ("B"):
           Answer3 = input("You find a tree and the bear is still running, what do you do?\nA) Climb the tree\nB) Hide behind the tree\n>? ")
           if Answer3 == ("A"):
               print("You went up the tree, and the bear went away!")
               print("You walked home and went to bed")
           elif Answer3 == ("B"):
               Answer4 = input("You fell down a hill and slid into a village, what do you do?\nA) Trade with a villager\nB) Head home\n>? ")
               if Answer4 == ("A"):
                   Answer5 = input("There is only one merchant, will you trade?\nA) Yes\nB) No")
                   if Answer5 == ("A"):
                       print("You traded two gold coins for a sword made of strong steel")
                       print("You headed home")
                   elif Answer5 == ("B"):
                       print("You did not trade, so you headed home")
                   else:
                       invalid()
               elif Answer4 == ("A"):
                   print("You headed home")
               else:
                   invalid()
           else:
               invalid()
    else:
        invalid()
game()