1

I want the loop to end when unit is 1 or 2, but code doesn't recognize even when the input is 1 or 2.

print("Calculate how much water you should drink a day!")
unit = int(input("Would you like to use the imperial or metric system? Write 1 for imperial and 2 for metric.\n"))
while unit != 1 or unit != 2:  
     print("Write either 1 or 2 to choose.")
     unit = int(input("Would you like to use the imperial or metric system? Write 1 for imperial and 2 for metric.\n"))
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • This is a duplicate of [Python \`if\` statement with \`or\` and \`!=\` condition](https://stackoverflow.com/questions/62257985/python-if-statement-with-or-and-condition) – mkrieger1 Sep 24 '22 at 10:24

2 Answers2

1

Because if unit == 1 it is !=2 so your condition is always true.

Use not in (1, 2) instead.

print("Calculate how much water you should drink a day!")
unit = int(input("Would you like to use the imperial or metric system? Write 1 for imperial and 2 for metric.\n"))
while unit not in (1, 2):
    print(unit)
    print("Write either 1 or 2 to choose.")
    unit = int(input("Would you like to use the imperial or metric system? Write 1 for imperial and 2 for metric.\n"))
bitflip
  • 3,436
  • 1
  • 3
  • 22
-1
print("Calculate how much water you should drink a day!")
unit = int(input("Would you like to use the imperial or metric system? Write 1 for imperial and 2 for metric.\n"))
while unit != 1 and unit != 2:  
     print("Write either 1 or 2 to choose.")
     unit = int(input("Would you like to use the imperial or metric system? Write 1 for imperial and 2 for metric.\n"))