I am doing an online Python course and became stuck on this exercise (sorry for the auto-translate, I am not good in English):
Consider the task of checking spam in an e-mail or filtering prohibited words in a message.
Let the
is_spam_words
function accept a string (parametertext
), check it for the content of prohibited words from the list (parameterspam_words
), and return the result of the check:True
, if there is at least one word from the list, andFalse
, if no stop words are found in the text.The words in the
text
parameter can be in any case, which means that theis_spam_words
function, when searching for prohibited words, is case-independent and all text must be lowercase. For simplicity, let's assume that there is only one forbidden word in the line.Provide a third parameter
space_around
to the function, which defaults toFalse
. It will be responsible for whether the function will search for a single word or not. A word is considered to stand alone if there is a space symbol to the left of the word or it is located at the beginning of the text, and there is a space or a period symbol to the right of the word.For example, we are looking for the word "rain" in the text. So in the word "rainbow" the challenge and result will be as follows:
print(is_spam_words("rainbow", ["rain"])) # True print(is_spam_words("rainbow", ["rain"], True)) # False
That is, in the second case, the word is not separate and is part of another.
In this example, the function will return
True
in both cases.print(is_spam_words("rain outside.", ["rain"])) # True print(is_spam_words("rain outside.", ["rain"], True)) # True
My code is:
def is_spam_words(text, spam_words, space_around=False):
if space_around:
text = text.lower()
for char in spam_words:
char.lower()
if text.find(spam_words) != -1 or text.startswith(spam_words):
return True
else:
return False
return False
The problem is:
The function returned an incorrect result for two parameters:
False
. Must beis_spam_words('rain outside.', ['rain']) == True
I have tried changing the main loops (like example1: if spam_words in text is True
or example2: if (" " + spam_word + " ") in (" " + text + " ")
), but I still do not understand why it is not working. I am expecting, that if spam word is found the word/text, the result will be True
and False
if it is not found.