-3

我刚刚开始使用 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")
4

2 回答 2

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")
于 2014-10-21T15:59:12.317 回答
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")
于 2019-12-22T05:03:26.197 回答