2015-09-18 117 views
-2

提示:中混合颜色编程

的颜色红,蓝和黄的被称为原色,因为它们 不能通过混合其他颜色制成。当您混合两种原色时,您获得 次要色彩: 当您混合红色和蓝色时,您会变成紫色。 当你混合红色和黄色时,你会变得橙色。 当你混合蓝色和黄色时,你会变绿。

设计一个程序,提示用户输入两个原色 一次一个的名称。如果用户输入“红色”,“蓝色”或 “黄色”以外的任何内容,则程序应打印“您没有输入两个原色。” 否则,它应该打印格式如下的东西:

“当你混合红色和蓝色时,你会变成紫色。” (假设用户输入“红”“蓝”。)

我的计划一直得到错误的标准输出

这是我写的:

primary_colora = input("Enter primary color:") 
primary_colorb = input("Enter primary color:") 
primary_colors = primary_colora or primary_colorb 

if primary_colora == (red, blue, yellow): 
    primary_colora = True 

elif primary_colorb == (red, blue, yellow): 
    primary_colorb = True 

elif primary_colors == red or blue: 
    print("When you mix red and blue, you get purple") 

elif primary_colors == yellow or blue: 
    print("When you mix yellow and blue, you get green") 

elif primary_colors == yellow or red: 
    print("When you mix yellow and red, you get orange") 

else: print("You didn't input two primary colors.") 

回答

1

你需要改变你的陈述相匹配颜色为字符串,而不是作为变量 -

if primary_colora in ['red', 'blue', 'yellow']: 

等等...

+5

这就是众多问题之一... Im相当肯定'primary_colors = primary_colora或primary_colorb'做不要做他认为的事...... –

0

我环顾四周,发现更简单的方法来组合和&或声明

这里的代码

primary_color1 = input("Enter primary color:") 
primary_color2 = input("Enter primary color:") 

if (primary_color1 == "red" and primary_color2 == "blue") or (primary_color1 == "blue" and primary_color2 == "red"): 
    print("When you mix red and blue, you get purple.") 

elif (primary_color1 == "blue" and primary_color2 == "yellow") or (primary_color1 == "yellow" and primary_color2 == "blue"): 
    print("When you mix blue and yellow, you get green.") 

elif (primary_color1 == "yellow" and primary_color2 == "red") or (primary_color1 == "red" and primary_color2 == "yellow"): 
    print("When you mix yellow and red, you get orange.") 

else: print("You didn't input two primary colors.")