Python Forum
Looping a question until correct feedback is given
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping a question until correct feedback is given
#1
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:
   

When I answer 'no', the program exits and works how I want it:
   

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.
   

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!
likes this post
Reply
#2
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.
rob101 and like this post
Reply
#3
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:
   

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?
Reply
#4
You now have another input inside of the loop but it does not alter the response variable.
TylerTrunzo likes this post
Reply
#5
(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?
TylerTrunzo likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
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')
Reply
#7
I got it! I appreciate all your help!
rob101 likes this post
Reply
#8
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
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!
Reply
#10
It's a fair enough technique, one I use myself; I simply use a (slightly) different while: clause.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Keeps looping even after correct answer mcesmcsc 2 1,915 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  Python Looping Gurus, question for you,... pcsailor 5 2,910 Sep-19-2018, 06:19 AM
Last Post: pcsailor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020