Python 定义if中的案例
我想定义一个与if-so一起使用的函数,如下所示:Python 定义if中的案例,python,if-statement,function,Python,If Statement,Function,我想定义一个与if-so一起使用的函数,如下所示: 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("Do
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.")
您可以将两个参数都设置为小写并进行比较:
def contains(txt, pattern):
return pattern.lower() in txt.lower()
问题是什么?你如何创建你的函数,它被称为什么,与它的使用方式无关-一个像myfunc:一样使用的函数和一个写为result=myfunc的函数没有什么不同。我是否正确地假设这是Python 3?如果是Python2,您可能希望在这里使用原始输入而不是输入。