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]}.")
添加回答
举报