2

Here is my code so far, which works:

print('Welcome to choose your own adenture.')
print('You enter a room with two doors.  Do you want to enter the red door or the white door?')
door = input()

while True:

  if door == 'white':
    print("there was a dragon behind the door.  It ate you and you died.")
    break
  elif door == 'red':
    run_or_dead = input("there is a bear behind the door.  Do you want to wave your arms and shout, or play dead?")
    while True:
      if run_or_dead == 'wave':
        print("Nice try, but the bear did't buy it.  It ate you and you died.")
        break
      elif run_or_dead == 'play dead':
        print("Nice try, but the bear did't buy it.  It ate you and you died.")
        break
      else:
        run_or_dead = input('Not a valid answer.  Do you want to wave or play dead?')
    break

  else:
    door = input('that is not a valid choice.  Do you want to enter the red door or the white door?')

I can't figure out how to get the program to ask the user if they want to repeat the whole process and do so if they enter 'yes'

Baxter
  • 21
  • 1
  • 2

2 Answers2

2

One simple way is to surround your while True with another loop, for example:

play_more = True
while play_more:
    # your while True loop goes here
    uinput = input('Type yes if you wanna play more')
    play_more = uinput.lower() == 'yes'
Mohd
  • 5,523
  • 7
  • 19
  • 30
2

You could put the game into a function, and call it as long as the user enters a predefined value:

def game():
    print('Welcome to choose your own adenture.')
    print('You enter a room with two doors.  Do you want to enter the red door or the white door?')
    door = input()

    if door == 'white':
        print("there was a dragon behind the door.  It ate you and you died.")
        return 0
    elif door == 'red':
        run_or_dead = input(
            "there is a bear behind the door.  Do you want to wave your arms and shout, or play dead?")
        while True:
            if run_or_dead == 'wave':
                print("Nice try, but the bear did't buy it.  It ate you and you died.")
                return 0
            elif run_or_dead == 'play dead':
                print("Nice try, but the bear did't buy it.  It ate you and you died.")
                return 0
            else:
                run_or_dead = input('Not a valid answer.  Do you want to wave or play dead?')

    else:
        door = input('that is not a valid choice.  Do you want to enter the red door or the white door?')
        return 0

while input('Do you want to play a game? [y/n]') == 'y':
    game()

Since it's a function, you can't use break anymore to stop the game, but just a return will do the same.

  • There seems to be a problem with this when you run it. It works fine for most answers, but when you put a non-valid answer for the door question, it loops back to the beginning after you put a second answer in. Try putting a non-valid answer in for the door question so you can see what I mean. – Baxter Jul 13 '17 at 20:55