您好我是编程新手,我正在编写一个程序,要求用户输入颜色。然后程序将告诉用户该颜色是否是油漆,光线,两者或两者的主要颜色。
原色为红色,蓝色和黄色
光的基色是红色,蓝色和绿色。
到目前为止,这是我的代码:
a = input("Enter Colour: ")
if 'Yellow' in a:
print('Yellow is a primary coulour for paint.')
elif 'Green' in a:
print('Green is a primary colour for light.')
elif 'blue' in a:
print('blue is a primary colour for light and paint.')
elif 'red' in a:
print('red is a primary colour for light and paint')
else:
print(a,'is not a primary colour.')
我使用此代码的问题是当我使用不同的情况时。 例如,输入蓝色应提供与Blue或BLUe相同的输出。
所以我希望程序忽略这个案例。
任何事情都会有所帮助。
答案 0 :(得分:2)
试试这个:
a = input("Enter Colour: ").lower()
if 'yellow' in a:
print('Yellow is a primary coulour for paint.')
elif 'green' in a:
print('Green is a primary colour for light.')
elif 'blue' in a:
print('blue is a primary colour for light and paint.')
elif 'red' in a:
print('red is a primary colour for light and paint')
else:
print(a,'is not a primary colour.')
通过将输入转换为小写,您可以只检查颜色的小写版本。
答案 1 :(得分:1)
尝试:
a = input("Enter Colour: ").strip()
color = a.lower()
if color in ["red", "blue"]:
print(color, "is a primary colour for light and paint.")
elif color in ["yellow"]:
print("Yellow is a primary colour for paint.")
elif color in ["green"]:
print("Green is a primary colour for light.")
else:
print(a, "is not a primary colour.")