Why doesn't Python recognize my correct input in the loop?

Viewed 23

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"))
2 Answers

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"))
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"))
Related