This question already has an answer here:
-
Asking the user for input until they give a valid response
17 answers
Python newbie here so sorry for what I'm sure is a stupid question, but I can't seem to solve the following challenge in a tutorial that is asking me to use a while loop to check for valid user input.
(using Python2.7)
Here's my code, but it's not working properly:
choice = raw_input('Enjoying the course? (y/n)')
student_surveyPromptOn = True
while student_surveyPromptOn:
if choice != raw_input('Enjoying the course? (y/n)'):
print("Sorry, I didn't catch that. Enter again: ")
else:
student_surveyPromptOn = False
The above prints out to the console:
Enjoying the course? (y/n) y
Enjoying the course? (y/n) n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n)
Which obviously isn't correct — the loop should end when the user enters either 'y' or 'n' but I'm not sure how to do this. What am I doing wrong here?
Note: the challenge requires me to use both the !=
operator and the loop_condition
A shorter solution
while raw_input("Enjoying the course? (y/n) ") not in ('y', 'n'):
print("Sorry, I didn't catch that. Enter again:")
What your code is doing wrong
With regard to your code, you can add some print as follow:
choice = raw_input("Enjoying the course? (y/n) ")
print("choice = " + choice)
student_surveyPromptOn = True
while student_surveyPromptOn:
input = raw_input("Enjoying the course? (y/n) ")
print("input = " + input)
if choice != input:
print("Sorry, I didn't catch that. Enter again:")
else:
student_surveyPromptOn = False
The above prints out:
Enjoying the course? (y/n) y
choice = y
Enjoying the course? (y/n) n
choice = y
input = n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
choice = y
input = x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n)
As you can see, there is a first step in your code where the question appears and your answer initializes the value of choice
. This is what you are doing wrong.
A solution with !=
and loop_condition
If you have to use both the !=
operator and the loop_condition
then you should code:
student_surveyPromptOn = True
while student_surveyPromptOn:
choice = raw_input("Enjoying the course? (y/n) ")
if choice != 'y' and choice != 'n':
print("Sorry, I didn't catch that. Enter again:")
else:
student_surveyPromptOn = False
However, it seems to me that both Cyber's solution and my shorter solution are more elegant (i.e. more pythonic).
You can use the condition
while choice not in ('y', 'n'):
choice = raw_input('Enjoying the course? (y/n)')
if not choice:
print("Sorry, I didn't catch that. Enter again: ")
The very simple solution for this is to initialize some variable at the before the loop kicks in:
choice=''
#This means that choice is False now
while not choice:
choice=input("Enjoying the course? (y/n)")
if choice in ("yn")
#any set of instructions
else:
print("Sorry, I didn't catch that. Enter again: ")
choice=""
What this while conditional statement means is that as long as choice variable is false--doesn't have any value means choice=''-- ,then proceeds #with the loop
If the choice have any value then proceed with enters the loop body and check
the value for specific input, if the input doesn't fulfill the required value
then reset the choice variable to False value again to continue prompts user
until a correct input is supplied