3

I've searched everywhere for an answer to my question, and I still can't figure it out! The answer is probably sooooo simple but I just can't get it, maybe because I'm just getting back into Python...

Anyway, I want to create a while-loop so that until the user enters 'y' or 'n' the question will keep being asked. This is what I have:

while True:   # to loop the question
    answer = input("Do you like burgers? ").lower()
    if answer == "y" or "n":
        break 

I'm honestly so baffed, so I beg for someone's help :)

1
  • 1
    if answer in ("y", "n"): Commented Sep 6, 2015 at 22:22

2 Answers 2

5
while True:   # to loop the question
    answer = input("Do you like burgers? ").lower()
    if answer == "y" or answer == "n":
        break 
Sign up to request clarification or add additional context in comments.

1 Comment

If you wonder why this works: in Python, strings have a truth value true. A non-empty string is always true: bool("foo") == True, so your code will always break because of ... or "n" which corresponds to ... or True.
3

Your condition is wrong. Also if you are using Python 2.x you should use raw_input (otherwise you would need to type "y" instead of y, for example):

while True:   # to loop the question
    answer = raw_input("Do you like burgers? ").lower()
    if answer == "y" or answer == "n":
        break

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.