用于在python中搜索字符串的函数
我正在尝试编写一个函数来执行以下操作:用于在python中搜索字符串的函数,python,function,Python,Function,我正在尝试编写一个函数来执行以下操作: 编写一个函数isRed(),该函数接受字符串参数并查找名为text的字符串中是否存在单词“red” 如果找到,则返回布尔值True,否则返回False 使用文本中的值输出调用函数的结果 这就是我所拥有的: def isRed(text): if text.find(red): return (True) else: return (False) return red 我得到这个错误: Program Failed f
def isRed(text):
if text.find(red):
return (True)
else:
return (False)
return red
我得到这个错误:
Program Failed for Input: ""
Expected Output: False
Your Program Output:
我对函数还不熟悉,对于为什么这不起作用,我有点困惑 你想要什么
def is_red(text):
if "red" in text:
return True
else:
return False
print str(is_red(text))
指令说明该函数应该返回
True
或False
。您的函数打印它们,但不返回它们。