0

I am writing my first python program (number guessing game) and I'm trying to ask if they are ready to play. If they answer yes, I want the program to continue. If they answer no, I want the program to either quit functioning, or asking them again if they are ready, with 'yes' being the only valid input that continues the code. If any other input is entered, I want the program to loop back on the question until 'yes' is input.

My original code was:

question = input("Do you want to try a guessing game?: [yes or no]")
if question != "yes": 
    exit("Okay, come back when you are ready!")
if question == "yes":
    print("Great! Guess a number from 0-20")

and when I input anything except yes, the program does this: program continues no matter the input

I tried changing it to a while statement such as:

response = input("Do you want to try a guessing game?: [yes or no]")
while response:
  if response =="yes":
    print("Great, let's get started!")
    break
  elif response == "no":
    print("Okay, Are you ready now?:")
  else:
    print("Sorry, please type 'yes' or 'no':")
    break

However, when I input anything except yes, the statement "Okay, are you ready now?" repeats over and over.

endlessly repeating statement

I am looking for a way for the input of anything except 'yes' to loop back to the statement 'Okay, are you ready now?' and continue seeking input until the answer is 'yes'. I feel like I am so close but can't figure it out. Any help would be appreciated

  • The second variant doesn't change "response" in the while-loop. You have to move the "input" in the loop to change that. You can make it an infinite loop with "while True:" and "break" or as needed. – Michael Butscher Oct 30 '22 at 20:10
  • What is it that loops? The body of the `while` block. What's inside there? Only `print` statements. There is nothing inside of the loop which would change `response` (for example by asking the user again)! – CherryDT Oct 30 '22 at 20:10

0 Answers0