0

I'm making a text-based adventure game for school, I've made this loop so if you don't give the answers allotted by the program, it will ask you over and over until you give it what it wants. I've made it loop, but I don't know how to exit the loop?

playerchoice =""
while (playerchoice != "wake" or "remain asleep" ):
       playerchoice = input("Do you wake, or remain asleep? wake/remain asleep: ")
       if playerchoice == "wake":
              print ("'Finally! I was starting to think I'd need to call the undertaker. Try not falling asleep in my front yard next time you're feeling tired?'")
       elif playerchoice == "remain asleep":
              print ("'Are you dead?' they ask again, kicking you in the leg this time. Reluctantly, you sit up. 'Oh, good.'")
       else:
              print ("Please type a valid answer.") 
lewsie
  • 5
  • use `return` keyword in python – Viraj Doshi Sep 30 '21 at 13:55
  • 6
    the keyword you are looking for is called `break` – yedpodtrzitko Sep 30 '21 at 13:56
  • Take look at `break` – Daweo Sep 30 '21 at 13:56
  • 2
    If the condition in the `while` was correct, a `break` wouldn't be needed: `while playerchoice != "wake" and playerchoice != "remain asleep":` Or also: `while playerchoice not in ["wake", "remain asleep"]:` – 001 Sep 30 '21 at 13:58
  • I'm not sure I follow your intent correctly. In your case the loop should break itself when the player's choice is `wake` or `remain asleep`, right? Why do you want an early exit? Do you want to introduce a `quit` choice? Do you want to limit the number of times a player can submit their choice? – Matias Cicero Sep 30 '21 at 13:59
  • Does this answer your question? [How to break out of multiple loops?](https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops) – wovano Sep 30 '21 at 14:11

2 Answers2

0

Another way is the following

choices = {
    "wake": "'Finally! I was starting to think I'd need to call the undertaker. Try not falling asleep in my front yard next time you're feeling tired?'",
    "remain asleep": "'Are you dead?' they ask again, kicking you in the leg this time. Reluctantly, you sit up. 'Oh, good.'"
    }
playerchoice = ""

while (playerchoice not in choices):
    playerchoice = input(
        "Do you wake, or remain asleep? wake/remain asleep: ")
    try:
        print(choices[playerchoice])
    except KeyError:
        print("Please type a valid answer.")

possible choices are in a dictionary,

you simply check your loop for not valid answers.

Etienne Dijon
  • 1,093
  • 5
  • 10
0

you should use break to exit loop when desired input is given.

playerchoice =""
while True:
       playerchoice = input("Do you wake, or remain asleep? wake/remain asleep: ")
       if playerchoice == "wake":
              print ("'Finally! I was starting to think I'd need to call the undertaker. Try not falling asleep in my front yard next time you're feeling tired?'")
              break
       elif playerchoice == "remain asleep":
              print ("'Are you dead?' they ask again, kicking you in the leg this time. Reluctantly, you sit up. 'Oh, good.'")
              break
       else:
              print ("Please type a valid answer.")