Help for the loop


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/0/4?curriculum_id=4f89dab3d788890003000096#

This is my code:
choice = raw_input('Enjoying the course? (y/n)')

while choice != "y" or choice != "n": # Fill in the condition (before the colon)
choice = raw_input("Sorry, I didn't catch that. Enter again: ")

The output is:
Enjoying the course? (y/n) h
Sorry, I didn't catch that. Enter again: d
Sorry, I didn't catch that. Enter again: y
Sorry, I didn't catch that. Enter again: n
Sorry, I didn't catch that. Enter again:

Although I put "y" or "n", it still runs "Sorry...."

Can you explain me what's wrong in my code lines?

BR,
Hoa


Replace this line with your code.


#2

agree that a loop runs as long as the condition is true?

so, with your loop, lets say i enter something valid, lets say y, then we get:

while false or true:

false or true is true, so the loop continues running

if the user enters something invalid, you get: true or true, which is also true, so your loop never ends. How do you think you can overcome this problem?


#3

Yeap. My conditions are incorrect. they should be FALSE and TRUE/FALSE.

Thanks for clarifying this! Really helps me!

BR
Hoa


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.