2015-09-18 118 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.")