1

so I have a problem that asks me for two primary colors and tells me the color they create, however when I enter the two primary colors I keep getting the error message that I put in if they don't enter two primary colors. Here is what I have.

red = '1'
blue = '2'
yellow = '3'

p1 = (input('Enter primary color: '))
p2 = (input('Enter primary color: '))

if p1 == '1' and p2 == '2':
    print('When you mix red and blue, you get purple')
elif p1 == '3' and p2 == '1':
    print('When you mix yellow and red, you get orange')
elif p1 == '2' and p2 == '3':
    print('When you mix blue and yellow, you get green')
else:
    print('You didnt input two primary colors.')
smci
  • 32,567
  • 20
  • 113
  • 146
seedotseedot
  • 11
  • 1
  • 3

2 Answers2

0

It seems you're using Python 2.x. Replace the occurrences of input with raw_input and you will achieve your goal.

Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42
  • 2
    Also, take a look at https://docs.python.org/2/library/functions.html#input, notice that it's equivalent to `eval(raw_input(prompt))` basically the `eval` call results in the input being converted to an integer, so if you type 1 for p1, you're getting an if clause that is trying to evaluate `1 == '1'` which is false. – photoionized Sep 30 '15 at 21:28
  • [`raw_input()` goes away on Python 3.x, replaced by `eval(input())`](http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x). – smci Sep 30 '15 at 21:31
  • @smci You are right. However, I'm assuming he's using Python 2.x – Amaury Medeiros Sep 30 '15 at 21:33
  • @photoionized Yes, another option would be changing the type of the constants to int and also change the if conditions. – Amaury Medeiros Sep 30 '15 at 21:34
0

I'm treating this as a python 3 problem, as you have tagged both.

Your problem is that when you type in your primary colour, you are entering "red", "yellow" or "blue". Python doesn't recognise these as values, as p1 is being set to "red" not "1", thus the comparison will not work.

This code should work:

p1 = (input('Enter primary color (red: 1, blue: 2, yellow: 3): '))
p2 = (input('Enter primary color (red: 1, blue: 2, yellow: 3): '))

if p1 == '1' and p2 == '2':
    print('When you mix red and blue, you get purple')
elif p1 == '3' and p2 == '1':
    print('When you mix yellow and red, you get orange')
elif p1 == '2' and p2 == '3':
    print('When you mix blue and yellow, you get green')
else:
    print('You didnt input two primary colors.')

EDIT: the below code does as you have asked

p1 = input('Enter primary color (red, blue, yellow): ')
p2 = input('Enter primary color (red, blue, yellow): ')

red = 'red'
blue = 'blue'
yellow = 'yellow'

if p1 == red and p2 == blue:
    print('When you mix red and blue, you get purple')
elif p1 == yellow and p2 == red:
    print('When you mix yellow and red, you get orange')
elif p1 == blue and p2 == yellow:
    print('When you mix blue and yellow, you get green')
else:
    print('You didnt input two primary colors.')
Liwa
  • 116
  • 12
  • that works but how do I get the input to read exactly, Enter Primary Color: and still get the results your code gave me – seedotseedot Sep 30 '15 at 22:08