我想定义一个与if
一起使用的函数,所以像这样:
def containing(a, b):
a == b.upper() or a == b.lower() or a == b.title() or a == b
c = input("Are you crying or smiling now? ")
if containing(c, "sMILING"):
print("Great!")
elif containing(c, "cRYING"):
print("Don't cry.")
而不是:
c = input("Are you crying or smiling now? ")
if c == "SMILING" or c == "smiling" or c == "Smiling" or c == "sMILING":
print("Great!")
elif c == "CRYING" or c == "crying" or c == "Crying" or c == "cRYING":
print("Don't cry.")
答案 0 :(得分:4)
您可以将两个参数都设为小写并进行比较:
def contains(txt, pattern):
return pattern.lower() in txt.lower()