3

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

martineau
  • 119,623
  • 25
  • 170
  • 301
AdjunctProfessorFalcon
  • 1,790
  • 6
  • 26
  • 62

3 Answers3

6

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: ")
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

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

martineau
  • 119,623
  • 25
  • 170
  • 301
Flabetvibes
  • 3,066
  • 20
  • 32
  • Thank 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? – AdjunctProfessorFalcon May 15 '15 at 20:51
  • You're welcome. Which code are you talking about? My solution with `!=` and `loop_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? – Flabetvibes May 15 '15 at 21:10
0

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