1

The code asks user to pick letter a, b, or c.

while True:
    user_input1 = input("Please choose a, b , c: ")
    if user_input1 == "a" or "b" or "c":
        break
print("This should be printed when user types in a, b, or c.")

The problem comes in when I don't type a, b , or c the loop breaks and prints the statement anyway. If I type in number 2 for an example, the code executes the break and prints the statement which isn't suppose to cause it's not letter a, b , or c.

I tried put the input variable outside the while loop but it still happens.

  • Your condition needs to be changed to `if user_input1 == "a" or user_input1 == "b" or user_input1 == "c"`. Although there is a better way to implement this logic using lists and the `in` keyword. – Gabe Morris Jan 26 '22 at 00:35
  • you're doing ` if user_input1 == "a" or "b" or "c":` where "b" is considered as true, and "c" too. you need to do: ` if (user_input1 == "a") or (user_input1 == "b") or (user_input1 == "c"):` or better: `if user_input1 in ["a","b","c"]:` – Ulises Bussi Jan 26 '22 at 00:36
  • 1
    Does this answer your question? [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value) oe https://stackoverflow.com/questions/3260057/how-to-check-variable-against-2-possible-values/3260070 – JonSG Jan 26 '22 at 00:44
  • 1
    This is probably the most commonly asked python question. You all with 5 figure reps know this is a duplicate and should not be answering. – JonSG Jan 26 '22 at 00:45
  • Yes adding parenthesis () in the if statement fixed it. It worked when it went from user_input1 == "a" or "b" or "c": to user_input1 == ("a" or "b" or "c"): . Why does adding parenthesis fix it? – iriandurian Jan 26 '22 at 01:02
  • 1
    I don't think it would actually fix it. It will only work for "a" – Sash Sinha Jan 26 '22 at 01:24

2 Answers2

2
while True:
    user_input1 = input("Please choose a, b , c: ")
    if user_input1 in [ "a" , "b" ,"c"]:
        break
print("This should be printed when user types in a, b, or c.")
M. Twarog
  • 2,418
  • 3
  • 21
  • 39
0

you are condition is wrong or 'b' is not comparing input with it , so 'b' is always true or 'c' , you need to change it to :

while True:
    user_input1 = input("Please choose a, b , c: ")
    print(user_input1)
    if user_input1  in ["a","b","c"]:
        break
print("This should be printed when user types in a, b, or c.")
eshirvana
  • 23,227
  • 3
  • 22
  • 38