根据@knitti的评论,问题是你需要先将句子分成单词,然后检查:
term = "message" #term we want to search for
input = raw_input() #read input from user
words = input.split() #split the sentence into individual words
if term in words: #see if one of the words in the sentence is the word we want
do_stuff()
否则,如果你有句子“那是经典的”并且你试图检查它是否包含单词“ass”,它将错误地返回True.
当然,这仍然不是完美的,因为那时你可能不得不担心像删除标点符号和什么不是(比如,等等),因为否则句子“那是一个经典的”.搜索“经典”时仍会返回False(因为最后的句号).而不是重新发明轮子,这是一篇关于从Python句子中删除标点符号的好帖子:
还需要考虑区分大小写,因此您可能希望在执行搜索之前将raw_input结果和搜索项更改为小写.你可以通过在str类上使用lower()函数轻松地做到这一点.
这些问题似乎总是很简单……