Quinten
Quinten

Reputation: 21

Is there a python function for filtering out certain words in an if statement

I'm trying to make a discord "dad bot" that responds to commands. When anyone says "I'm" it will print out Hi "whatever they inputted" I'm dad bot and this worked fine but a lot of my friends put in "im" instead of "I'm" meaning I allowed that as well but now it triggers with words ending in "im" like him or aim.

This is the code I have tried, I have also tried cycling through the array using a for loop.

im = ["aim", "bim", "cim", "dim", "eim", "fim", "gim", "him", "iim", "jim", "kim", "lim", "mim", "nim", "oim", "pim", "qim", "rim", "sim", "tim", "uim", "vim", "wim", "xim", "yim", "zim"]

#part of a larger if statement 
elif ("i'm " in message.content.lower() or "im " in message.content.lower()) and not message.content.lower() in im:
    if "i'm" in message.content:
        you = message.content.split("i'm")
    elif "im" in message.content:
        you = message.content.split("im")
    elif "Im" in message.content:
        you = message.content.split("Im")
    else:
        you = message.content.split("I'm")
    await message.channel.send(f"""Hi{you.pop(1)} I'm Dadbot""")

I have also tried to use a for loop like this

if "im" in message.content:
                for x in im:
                    if im(x) in message.content:
                        go = 1
                        break
                    go = 0
            if go == 1:

The code should just ignore any other text and should only respond to "im", "Im", "I'm" and "I'm"

With something that looks like this :

User:

"I'm hungry"

Dad bot:

"Hi hungry I'm Dadbot"

This is the error that I get:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 251, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/User/PycharmProjects/dadBot/dadBot.py", line 26, in on_message
    elif ("i'm " in message.content.lower() or "im " in message.content.lower()) and not im in message.content.lower():
TypeError: 'in <string>' requires string as left operand, not list

Upvotes: 1

Views: 448

Answers (3)

mguven guven
mguven guven

Reputation: 91

m="Im hungry"
d=m.lower().replace("'","").split(' ')
if "im" in d:
    z=d[d.index("im")+1]
    print "hey " + z +" how are you?"

if what you have tried is catching the name, this approach also may work.

Upvotes: 0

backtrack
backtrack

Reputation: 8154

I think of a small regex like this ^(im|i\'m)\s+(.*)

example:

import re
ip = "im hungrey"
list_ip = re.match(r"^(im|i\'m)\s+(.*)",ip, re.IGNORECASE)
if list_ip:
   print('Hi '+ list_ip.group(2) + ' '+list_ip.group(1)+ ' Dadbot')

output:

i/p: "im hungrey"
o/p: Hi hungrey im Dadbot
i/p: "him hi"
o/p: null
i/p: "i'm all"
o/p: Hi all i'm Dadbot

Explanation:

  1. Use re - regex in python for pattern match
  2. I am matching the words and grouping the values
  3. group(1) is (im|i\'m) - it gives me First word
  4. group(2) is (.*) - it gives the remaining portion of input

Upvotes: 1

spiralarchitect
spiralarchitect

Reputation: 910

It works fine for me. I modified code to execute in my local. It looks like the problem is in message.content. I guess it outputs a list, not a string.

sample = "I'm hungry"

im = ["aim", "bim", "cim", "dim", "eim", "fim", "gim", "him", "iim", "jim", "kim", "lim", "mim", "nim", "oim", "pim", "qim", "rim", "sim", "tim", "uim", "vim", "wim", "xim", "yim", "zim"]

#part of a larger if statement
if ("i'm " in sample.lower() or "im " in sample.lower()) and not sample.lower() in im:
     if "i'm" in sample:
         you = sample.split("i'm")
     elif "im" in sample:
         you = sample.split("im")
     elif "Im" in sample:
         you = sample.split("Im")
     else:
         you = sample.split("I'm")
     print(f"""Hi{you.pop(1)} I'm Dadbot""")

Upvotes: 0

Related Questions