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

Edwarric
  • 513
  • 2
  • 9
  • 19

2 Answers2

5
while True:   # to loop the question
    answer = input("Do you like burgers? ").lower()
    if answer == "y" or answer == "n":
        break 
StephenG
  • 2,851
  • 1
  • 16
  • 36
  • 3
    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`. – jojonas Sep 06 '15 at 22:48
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
Salem
  • 12,808
  • 4
  • 34
  • 54