我需要编写此代码,以便人在输入中输入颜色或风味,无论是大写还是小写。为此,我用不同的方式写了一个颜色的字符串。但是,无论我将颜色或味道设置为小写还是大写,它始终显示为错误。如何显示TRUE?
null
答案 0 :(得分:0)
问题是您应该将所有颜色存储到list
中,并且仅将and
存储到这样的颜色中。而且您还应该从零开始学习如何定义类等
class class_apple:
color = ""
flavor = ""
Apple = class_apple()
Colors = ["Red", "red", "RED"]
Flavors = ["Sweet", "sweet", "SWEET"]
def Apple_color():
Apple.color = input("Insert the color of the apple: ")
if Apple.color in Colors:
print("TRUE")
else:
print("FALSE")
但是我建议使用lower
来字符串对象:
class class_apple:
def __init__(self):
self.color = ""
self.flavor = ""
Apple = class_apple()
Colors = ["red", "green", "blue"]
Flavors = ["sweet", "spicy"]
def Apple_color():
Apple.color = input("Insert the color of the apple: ")
if Apple.color.lower() in Colors:
print("TRUE")
else:
print("FALSE")