Python Forum

Full Version: Looping a question until correct feedback is given
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm writing my first python program as a number guessing game. I am trying to loop a question until a 'yes' or 'no' response is given but am having trouble figuring it out.
'''
response = input("Do you want to try a guessing game?: [yes or no]")
while response != "yes" or "no":
  if response =="yes":
    print("Great, let's get started! Guess a number from 0-20")
    break
  elif response == "no":
    print("Okay, see you later!")
    exit()
  else:
    print("Sorry, please type 'yes' or 'no':")
  break 
'''

When I answer 'yes', the program works fine and continues how I want it:
[attachment=2061]

When I answer 'no', the program exits and works how I want it:
[attachment=2062]

When I answer anything else, I want it to ask "Sorry, please type 'yes' or 'no" and continue asking that question until a 'yes' or 'no' input is received. However, it just continues with the program.
[attachment=2063]

I would like it to keep asking the question until a 'yes' or 'no' is received, but I can't quite figure it out. I would appreciate any help!
A clue would be that the question is only asked once before the loop begins, once in the loop only the answer is known the question is never asked again.
Okay, I think I am getting closer.

response = input("Do you want to try a guessing game?: [yes or no]")
while response != "yes" or "no":
  if response =="yes":
    print("Great, let's get started! Guess a number from 0-20")
    break
  elif response == "no":
    print("Okay, see you later!")
    exit()
  else:
    print("Sorry, please type 'yes' or 'no':")
    input ("Do you want to try a guessing game?: [yes or no]")
    continue
I am able to get the code to ask the question again:
[attachment=2065]

However, I cannot get it to recheck the input for yes, no, or something else, and act accordingly. The continue statement keeps asking the question and the break statement ends the program. Can I get another hint?
You now have another input inside of the loop but it does not alter the response variable.
(Oct-30-2022, 09:27 PM)TylerTrunzo Wrote: [ -> ]I would like it to keep asking the question until a 'yes' or 'no' is received

Or to put it another way, while the answer is not yes or no. Now do you see?
I often do that like this, because sometimes I need 1 file, other times another file.

answers = ['yes', 'no']
answer = 'X'
while not answer in answers:
    answer = input('Enter yes or no ... ')

if answer = 'yes':
    open('thisfile.txt')
else:
    open('thatfile.txt')
I got it! I appreciate all your help!
(Oct-30-2022, 11:43 PM)Pedroski55 Wrote: [ -> ]while not answer in answers:

Why not simply say while answer not in answers: ?

For me, least ways, it scans better.
Don't know, not being an expert! What goes on behind the scenes is a book with 7 seals for me!

Just know it works!

I often want to open either a blanko Excel file, or a file with data, so that's how I do it!

You can put any number of data in the answers list!
It's a fair enough technique, one I use myself; I simply use a (slightly) different while: clause.