#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」,「你好,當返回一個錯誤的布爾世界「) 真
,但是當我去找到一個茶重複類似於「l」或「o」的片段會返回錯誤。
isItThereS( 「L」, 「世界你好」) 假
我怎麼會去得到那個不返回false,而是因爲返回True字符在技術上是在字符串中?
因爲'True'的計算結果爲'1',因此如果事件發生> 1,那麼您將得到錯誤的輸出。你必須使用像str.find()這樣的內置方法來執行這樣的操作,或者如果你還想修改這段代碼,那麼使用'return not letInWord == False' – ZdaR