This code is supposed to give an error message, "Error please try again!" If the colors entered are not red, blue. or yellow. It's giving me the error message while also giving me the correct input and I cannot figure out why.
ColorTwo = input('Enter the second primary color please ')
if ColorOne != ('red' or 'blue' or 'yellow'):
print('Error please try again!')
if ColorTwo != ('red' or 'blue' or 'yellow'):
print('Error please try again!')
if ColorOne == ('red') and ColorTwo == ('blue'):
print('Purple')
if ColorOne == ('red') and ColorTwo == ('yellow'):
print('Orange')
if ColorOne == ('blue') and ColorTwo == ('yellow'):
print('Green')
if ColorTwo == ('red') and ColorOne == ('blue'):
print('Purple')
if ColorTwo == ('red') and ColorOne == ('yellow'):
print('Orange')
if ColorTwo == ('blue') and ColorOne == ('yellow'):
print('Green')```
CodePudding user response:
try this:
ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
if ColorOne not in ['red','blue','yellow']:
print('Error please try again!')
if ColorTwo not in ['red','blue','yellow']:
print('Error please try again!')
if ColorOne == ('red') and ColorTwo == ('blue'):
print('Purple')
if ColorOne == ('red') and ColorTwo == ('yellow'):
print('Orange')
if ColorOne == ('blue') and ColorTwo == ('yellow'):
print('Green')
if ColorTwo == ('red') and ColorOne == ('blue'):
print('Purple')
if ColorTwo == ('red') and ColorOne == ('yellow'):
print('Orange')
if ColorTwo == ('blue') and ColorOne == ('yellow'):
print('Green')
The problem with your code is with the logic, in real world (spoken language) you would say if the color is NOT red or blue or yellow then do something, but in programming logic this doesn't work like that. the != (red or blue or yellow) is equal to if the color is not red and not blue and not yellow then do something. And that's using de morgan laws.
CodePudding user response:
Solved it.
ColorTwo = input('Enter the second primary color please ')
if ColorOne not in ('red', 'blue', 'yellow'):
print('Error please try again!')
if ColorTwo not in ('red', 'blue', 'yellow'):
print('Error please try again!')
if ColorOne == ('red') and ColorTwo == ('blue'):
print('Purple')
if ColorOne == ('red') and ColorTwo == ('yellow'):
print('Orange')
if ColorOne == ('blue') and ColorTwo == ('yellow'):
print('Green')
if ColorTwo == ('red') and ColorOne == ('blue'):
print('Purple')
if ColorTwo == ('red') and ColorOne == ('yellow'):
print('Orange')
if ColorTwo == ('blue') and ColorOne == ('yellow'):
print('Green')
You'll need to check like this
if ColorTwo not in ('red', 'blue', 'yellow'):
CodePudding user response:
The problem is the code ColorOne != ('red' or 'blue' or 'yellow'):
. It is not the correct way to check if a variable is one of those values. You should change to ColorOne not in ('red', 'blue', 'yellow'):
Regardless, I did some improvements.
- applied strip and lower to the inputs.
- sorted the inputs to check a possible combination once.
- elif - if one condition is true, it is not necessary to check the others.
ColorOne = input('Enter the first primary color please ')
ColorTwo = input('Enter the second primary color please ')
Colors = ('red', 'blue', 'yellow')
if ColorOne not in Colors or ColorTwo not in Colors:
print('Error please try again!')
else:
combination = sorted((ColorOne.strip().lower(), ColorTwo.strip().lower()))
if combination == ['blue', 'red']:
print('Purple')
elif combination == ['red', 'yellow']:
print('Orange')
elif combination == ['blue', 'yellow']:
print('Green')