1 回答

TA貢獻1786條經驗 獲得超11個贊
您可以通過使用函數來減少重復(例如用于輸入顏色) 您可以通過使用以一對顏色作為鍵和混合顏色作為值的字典來簡化顏色混合。
為避免必須處理兩種顏色排列,請使用數組來存儲它們并對數組進行排序。這允許您的字典鍵僅關注按字母順序排列的顏色對。
下面是一個例子:
PRIMARY_COLORS = ["red", "blue", "yellow"]
mixes = { ("blue","red"):"purple", ("red","yellow"):"orange", ("blue","yellow"):"green" }
def inputColor(rank):
while True:
color = input("Enter the "+rank+" primary color in lower case letters: ").lower()
if color in PRIMARY_COLORS: return color
print("Error: the color entered is not a primary color.")
colors = tuple(sorted([inputColor("first"),inputColor("second")]))
if colors[0] == colors[1]:
print("Error: The two colors you entered are the same.")
elif colors in mixes:
print(f"When you mix {colors[0]} and {colors[1]}, you get {mixes[colors]}.")
添加回答
舉報