0

I need to write a code which allow you to order a pizza but if you enter something else then S (small) or M (medium) or L (large) it has to return you that question. but the problem is that if you type S it continues but if you enter M or L it returns the same question (which not should happen)

I tried to put a block before the while and I tried to put it on different lines.

Size_pizza = str(input("Which size do you want your pizza to be? Small(S), medium (M) of Large (L)"))
while Size_pizza != "K" or "M" or "G":
    Size_pizza = str(input("Which size do you want your pizza to be? Small(S), medium (M) of Large (L)"))

if you enter S or M or L it has to continue with the code but it only continues if you enter S.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
PSRemco
  • 11
  • 1
  • 1
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – jonrsharpe Jun 16 '19 at 12:41
  • FYI you should always tag with the actual language that you're using as an absolute minimum; it generally doesn't matter where you're running it. – jonrsharpe Jun 16 '19 at 12:42

2 Answers2

0

If you want to make sure the input is "S", "M", or "L", then your while loop should have a statement to check if the input is not "S", "M", or "L".

Size_pizza = str(input("Which size do you want your pizza to be?)) 
while Size_pizza != "S" or Size_pizza != "M" or Size_pizza != "L":
        Size_pizza = str(input("Which size do you want your pizza to be?))

Note that you have to say Size_pizza != "S" or Size_pizza != "M" or Size_pizza != "L".... not just Size_pizza != "S" or "M" or "L".

stevestar888
  • 113
  • 7
  • you forgot " after 'Which size do you want your pizza to be' and because of that in the next line strings look like not strings and other things like strings – Igor sharm Jun 20 '19 at 13:50
0

Size_pizza != "K" or "M" or "G" will always evaluate to True, because even if the first part of alternative is False, the rest will always evaluate to True - "M" and "G" are always True, because they're non-empty strings.

To make it work as intended, there are two possible ways to fix:

while Size_pizza != "S" or Size_pizza != "M" or Size_pizza != "L":
    ...

or

while Size_pizza not in ("S", "M", "L"):
    ...

I'd prefer the second one since it's more compact.

asikorski
  • 882
  • 6
  • 20