'What is wrong with my loop? Absolute beginner [duplicate]
while True:
COLOR = input('Please enter green or blue: (green/blue) ').lower()
if COLOR == 'blue' or 'green':
print('correct!')
break
else:
print('Please enter green or blue.')
continue
Terminal: Please enter green or blue: (green/blue) asdfasefase correct!
even though the input does not match the variable it still is true, why?
Solution 1:[1]
your if statement is wrong. It needs to be:
if COLOR == 'blue' or COLOR == 'green'
in your case or 'green' will always be interpreted as true hence anything will be correct
Solution 2:[2]
while True:
COLOR = input('Please enter green or blue: (green/blue) ').lower()
if COLOR == 'blue' or COLOR =='green':
print('correct!')
break
else:
print('Please enter green or blue.')
continue
try this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ĺ eky |
Solution 2 | Alioth Xolan |