爲什麼我的代碼底部的if
聲明不起作用? 單詞列表包含幾個「測試」,但在if
聲明下方的打印語句不起作用。爲什麼我的if語句不起作用?
text1 = "a"
text2 = "b"
text3 = "c"
words = []
if len(text1) < 2:
words.append('test11')
elif text1.isspace():
words.append('test12')
if len(text2) < 2:
words.append('test21')
elif text2.isspace():
words.append('test22')
if len(text3) < 2:
words.append('test31')
elif text3.isspace():
words.append('test32')
if "test" in words:
print "Test"
你'if'聲明是工作的罰款。沒有任何內容被打印出來,因爲你的清單「words」不包含字符串「test」。 – timgeb
「詞」列表中不包含確切的單詞'「test」'。可能是你用字符串 – kuro
混淆了這個,因爲''test''不在'單詞'中,它在單詞的一些單詞中,而不是單詞本身。你可以使用:'如果有的話([「用單詞測試」)''。雖然這有點羅嗦。 –