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).