0

I'm making a basic program to detect certain words from a string and mark the message as SPAM if the string contains the spam keywords but ran into a problem. The compiler doesn't detect it as spam unless I input the exact same strings.

Here's the code :

text = input("text : ")

if(text == 'make a lot of money' in text) or (text == 'buy now'in text) or (text == 'subscribe this 'in text) or (text =='click link' in text):
    print("SPAM")
else:
    print("OKAY")
3
  • 1
    (text == 'buy now' in text) is incorrect. Use ('buy now' in text). Same goes for the other 3 conditions Commented Mar 4, 2022 at 12:40
  • @Aziz I think it should be like 'buy now' in text Commented Mar 4, 2022 at 12:41
  • @GautamJangid Yes, it was a typo. Fixed :) Commented Mar 4, 2022 at 12:42

3 Answers 3

3

That' because you're comparing with equals:

text == 'make a lot of money' in text

instead just use the in command:

'make a lot of money' in text

will yield True if the text contains that string

Sign up to request clarification or add additional context in comments.

Comments

0

You're not using correct syntax for if statement, please use this:

text = input("text : ")

if 'make a lot of money' in text or 'buy now' in text or 'subscribe this' in text or 'click link' in text:
    print("SPAM")
else:
    print("OKAY")

Comments

0
text = input("text : ")

if('make a lot of money' in text) or ('buy now'in text) or ('subscribe this' in text) or ('click link' in text):
    print("SPAM")
else:
    print("OKAY")

OR

text = input("text : ")
spam_phrases = ['make a lot of money','buy now','subscribe this','click link']

for phrase in spam_phrases:
    if phrase in text:
        print("SPAM")
        break
else:
    print("OKAY")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.