While the loop is checking if the user input is correct?
Python newbie is so sorry I'm sure this is a stupid question, but I can't solve the following problem in a tutorial that asks me to use a while loop to validate user input.
(using Python2.7)
Here's my code, but it doesn't work as expected:
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 version is output 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 is obviously wrong - the loop should end when the user types "y" or "n", but I'm not sure how. What am I doing wrong here?
Note: the task requires me to use both operator !=
andloop_condition
Shorter solution
while raw_input("Enjoying the course? (y/n) ") not in ('y', 'n'):
print("Sorry, I didn't catch that. Enter again:")
What is your code doing wrong
As far as your code is concerned, you can add printing like this:
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 is:
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, in your code where the question comes up, there is a first step and your answer is initializing the value choice
. This is what you are doing wrong.
Solution with !=
andloop_condition
If you need to use both the operator !=
and loop_condition
, then you must enter the 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 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 simplest solution for this is to initialize some variable in a step before the loop starts:
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=""
The fact that this conditional operator means that as long as the choice variable is false - it does not matter means the choice = '' - then continues # with a loop If the choice has any value, then proceed to enter the body of the loop and check the value for a particular input if the input does not match the required value then reset the selection variable to False again to continue prompting the user until the correct input is set