-3

I am just starting my programming classes in python and have to write a program that lets the user input two of the primary colors and it prints the resulting secondary color. I understand most of the beginner steps of python but always seem to skip over a few things or miss a step here and there. Can someone tell me what I'm doing wrong?

color1 = input("Choose your first color. (red, blue, yellow) :")
color2 = input("What is your second color? (red, blue, yellow) :")

if color1 == red and color2 == blue or color1 == blue and color2 == red:
    print("Your result is purple")
elif color1 == red and color2 == yellow or color1 == yellow and color2 == red:
    print("Your result is orange")
elif color1 == blue and color2 == yellow or color1 == yellow and color2 == blue:
    print("your result is green")
straxus
  • 3
  • 1
  • 3
  • Why do *you think* you're doing something wrong? Do you get errors (provide full traceback)? Unexpected outputs (provide inputs and expected and actual outputs)? – jonrsharpe Oct 21 '14 at 15:10

2 Answers2

2

Enclose the colors in " ".

if color1 == red and color2 == blue or color1 == blue and color2 == red:
    print("Your result is purple")

In the above line of code(in your code) red without the quotes is considered as a variable. If you enclose them in quotes it will work.

if (color1 == "red" and color2 == "blue") or (color1 == "blue" and color2 == "red"):
        print("Your result is purple")
Beginner
  • 1,975
  • 8
  • 29
  • 45
0

I came across this example and it also specified that if the user selects anything other than "red", "blue" and "yellow" it must state "Error Occurred", in that case:

primary_color1 = input('Enter the primary colors: ')
primary_color2 = input('Enter the second primary color: ')

if (primary_color1 == "red" and primary_color2 == "blue") or (primary_color1 == "blue" and primary_color2 == "red"):
    print( "mix red and blue,get purple")
elif (primary_color1 == "red" and primary_color2 == "yellow") or (primary_color1 == "yellow" and primary_color2 == "red"):
    print( "mix red and yellow,get a orange")
elif (primary_color1 == "blue" and primary_color2 == "yellow") or (primary_color1 == "yellow" and primary_color2 == "blue"):
    print( "mix blue and yellow,get a green")
else:
    print("Error occured")
Dmitry Kuzminov
  • 4,427
  • 5
  • 12
  • 26
AnumT
  • 1