While loop to check for valid user input?
Solution 1
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).
Solution 2
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: ")
AdjunctProfessorFalcon
Updated on June 30, 2022Comments
-
AdjunctProfessorFalcon almost 2 years
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 theloop_condition
-
AdjunctProfessorFalcon almost 9 yearsThank you for the help! I tried the solution with the != and loop_condition but when user enters either 'y' or 'n', it asks the question again. How can I get the loop to quit after someone enters either 'y' or 'n' the first time the question is asked?
-
Flabetvibes almost 9 yearsYou're welcome. Which code are you talking about? My solution with
!=
andloop_condition
does not ask the question again after someone enters either'y'
or'n'
. I suppose you have tried something different. If so, please could you edit your question to add this piece of code?