The program I am writing below has the follow requirements:
# Design a program that prompts the user to enter the names of two primary colors
# to mix. If the user enters anything other than red, blue or yellow, the
# program should display an error message. Otherwise the program should display
# the name of the secondary color that results.
This is the code I have written - based upon a Java program I have wrote previously and evidently was way off for Python.:
print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')
red = False
blue = False
yellow = False
color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))
if color1 == red and color2 == blue:
print('That makes purple!')
elif color1 == blue and color2 == red:
print('That makes purple!')
elif color1 == yellow and color2 == red:
print('That makes orange!')
elif color1 == red and color2 == yellow:
print('That makes orange!')
elif color1 == blue and color2 == yellow:
print('That makes green!')
elif color1 == yellow and color2 == blue:
print('That makes green!')
else:
print('You did not enter a primary color!')
No matter what color combination I enter, I get the result "That makes purple!" Where did I go wrong with the logic of this program? Further, when I do not enter green as the primary color, I get this:
Traceback (most recent call last):
File "color.py", line 19, in <module>
color1 = bool(input('Enter your first primary color: \n'))
File "<string>", line 1, in <module>
NameError: name 'green' is not defined
instead of the error message "You did not enter a primary color!"
Where am I going wrong?
EDIT: This is my new code and it works other than the erroring out.
print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')
red = 1
blue = 2
yellow = 3
color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')
if color1 == 1 and color2 == 2:
print('That makes purple!')
elif color1 == 2 and color2 == 1:
print('That makes purple!')
elif color1 == 3 and color2 == 1:
print('That makes orange!')
elif color1 == 1 and color2 == 3:
print('That makes orange!')
elif color1 == 2 and color2 == 3:
print('That makes green!')
elif color1 == 3 and color2 == 2:
print('That makes green!')
else:
print('You did not enter a primary color!')
bool('red')? You'll always getTruebecause the string is non-empty.red,blue, andyelloware all set to the same value,False, but you expect there to be a difference in yourifconditions? Try to write pseudocode, then convert that into python.