#Function takes a character and a string and returns a boolean reflecting if
#the character is found in the string.
def isItThereS(letter, word):
letInWord = 0
for l in word:
if l == letter:
letInWord += 1
return letInWord == True
当我把它放在接线员的时候
isItThereS("h","hello world") True
但当我找到一个重复“l”或“o”的字符时,它返回false。在
isItThereS("l","hello world") False
我该如何让它不返回false,而是返回True,因为字符在字符串中?在
如果真的想为此使用自定义函数,请将返回值改为
return letInWord >= 1
。因为除了1 == True
之外的所有内容都将计算为False
。(因此,更合适的函数名是is_it_there_only_once
)。在否则请使用armak提供的解决方案。在
您可以简单地使用in运算符
相关问题 更多 >
编程相关推荐