为if中的案例定义

时间:2014-11-09 16:06:44

标签: python if-statement function

我想定义一个与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.")

1 个答案:

答案 0 :(得分:4)

您可以将两个参数都设为小写并进行比较:

def contains(txt, pattern):
    return pattern.lower() in txt.lower()