-1

I'm very new to programming and was doing a simple choice game:

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")

But how do I go on? Like add an option after having proceeded with a chopped of hand or running through the forest (2 choices at least for both previous outcomes)

ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
  • 1
    You can but an if/else inside an if/else as many times as you want. Just add another level of indentation. – Alex Hall Nov 26 '16 at 09:10
  • It sounds like you are looking for a looking for a decision tree architecture for your game. There are plenty of great resources discussing how to implement this. Wikipedia would be a good start. https://en.wikipedia.org/wiki/Decision_tree – rfj001 Nov 26 '16 at 09:44

3 Answers3

0

You could create different functions/procedures for different cases. For example:

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

This is just an example, but using functions you can create different options for different cases and call a function in other parts of your code. For example you character can lose his arm in a different situation and in this case you just need to recall you function choppedHand().

I hope this was what you were looking for.

Fabrizio A.
  • 64
  • 1
  • 8
0

Here is a start you can hopefully figure out how to expand on :)

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

Try the game out here

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • now the "else" after "player_crossedRoad" is somewhy invalid syntax? That's what I did: 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 if __name__ == "__main__":main() – Tristan Aljamaa Nov 26 '16 at 10:06
  • What is your question? – Sash Sinha Nov 26 '16 at 10:09
  • The completely random invalid syntax which looks just like the others and has a colon after it. – Tristan Aljamaa Nov 26 '16 at 10:13
  • Thanks but already got a fix earlier from my friend. But huge thanks for helping me - the game is ready :) – Tristan Aljamaa Nov 26 '16 at 17:39
  • Actually could you help me? In Python IDLE 3.4 it says "Answer" is not defined and I really cannot find a fix for it. https://repl.it/E8Uk Here it is (just used that webpage to a more comfortable way to show that to you) – Tristan Aljamaa Nov 26 '16 at 18:01
-1
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()
Rob Ert
  • 442
  • 5
  • 23
EvanR
  • 1
  • This needs a whole lot of formatting. Start the block with triple backticks and the programming language, like this: `\`\`\`python` make things multiple lines, and use two spaces per indent. – Samathingamajig Oct 22 '20 at 21:40