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!')
Your trouble exists in these lines:
color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))
When you do this, any non-empty value entered will result in True
. The only way you'd be able to get False
would be to hit return
at the prompt and submit an empty string. Your logic in how you want to handle what the user enters is a bit flawed. You might want:
if color1 == 'red'
After you drop the superfluous call to bool
, but that's just a suggestion.
color1 = bool(input('Enter your first primary color: \n'))
If we simplify this, we get
color1 = bool("red") #color1 becomes True
So now your comparisons are evaluating does False
equal True
- completely ignoring the colors you have inputted.
Try something like
RED = 'red'
BLUE = 'blue'
color1 = input('Enter your first primary color: \n').lower()
color2 = input('Enter your second primary color: \n').lower()
if color1 == RED and color2 == BLUE:
print('That makes purple!')
You have some big problems with your code. First, you call bool(input())
. As long as you provide some input, color1
and/or color2
are set to True.
Therefore, you are calling if True == False
... Your code does nothing to check the actual names of colors. Instead, I suggest using plain input()
to take in a ` string .
Here is your edited code:
print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')
color1 = input('Enter your first primary color: \n')
color2 = 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!')
You should be comparing color1
and color2
to strings not booleans:
color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')
if color1 == "red" and color2 == "blue":
print("That makes purple!")
The problem seems to lie in these lines
red = False
blue = False
yellow = False
color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))
when you input a color it is just being set to false, regardless of which color it is. to the computer this is happening(I'll use red and yellow as examples)
color1 = bool(red)
since you defined red earlier as False:
color1 = bool(False)
color1 = False
Similarly
color2 = bool(yellow)
yellow is also defined as False
color2 = bool(False)
color2 = False
So when we get to your first if statement
if color1 == red and color2 == blue:
the computer sees
if False==False and False == False:
which evaluates to True
if True and True:
if True:
Part of your problem might be arising from your use of input(). To python2.x(which I will assume you are using) input() evaluates whatever is entered. For example:
input("Enter Something")
>>>3
input("Enter Something Else")
>>>3.1
would return the integer value 3 and the float value 3.1 respectively. The computer looks at what you type in and tries its best to give you a piece of data that makes sense. In your case.
yellow = False
color1 = bool(input('Enter your first primary color: \n'))
>>>yellow
the computer searches the program's namespace for the value of the expression yellow, which in this program is False.
It may help you to use raw_input(), which does not evaluate what is typed in and simply returns the string value of whatever is typed. For example:
input()
>>>yellow
would evaluate to False but
raw_input()
>>>yellow
would evaluate to the string value "yellow"
Try defining your color constants as strings rather than bools and processing input as a string as well. so
yellow = "yellow"
rather than
yellow = False
I didn't use variables for the colors...I simply tested if the variable was=='color'
so my code was...
color1=input('Enter primary color:')
color2=input('Enter primary color:')
if color1=='red' and color2=='blue':
print("When you mix red and blue, you get purple.")
elif color1=='red' and color2=='yellow':
print("When you mix red and yellow, you get orange.")
elif color1=='blue' and color2=='red':
print("When you mix blue and red, you get purple.")
elif color1=='blue' and color2=='yellow':
print("When you mix blue and yellow, you get green.")
elif color1=='yellow' and color2=='red':
print("When you mix yellow and red, you get orange.")
elif color1=='yellow' and color2=='blue':
print("When you mix yellow and blue, you get green.")
else:
print("You didn't input two primary colors.")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.