混色器用户输入

时间:2014-10-21 15:09:02

标签: python if-statement colors mixer

我刚刚在python中启动我的编程类,并且必须编写一个程序,让用户输入两种主要颜色,然后打印生成的二次颜色。我理解python的大部分初学者步骤,但似乎总是跳过一些事情或者错过了一个步骤。有人能告诉我我做错了什么吗?

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")

2 个答案:

答案 0 :(得分:2)

将颜色括在" "中。

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

在上面的代码行中(在您的代码中){}没有引号的red被视为variable。如果将它们用引号括起来就可以了。

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

答案 1 :(得分:0)

我遇到了这个示例,它还指定了如果用户选择“红色”,“蓝色”和“黄色”以外的其他任何内容,则在这种情况下必须声明“发生错误”:

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")