tags:

views:

46

answers:

3

I'm writing a AI program in Python and want to save time when interacting with the bot. Instead of using this code:

if "how are you" or "How are you" in talk:
     perform_action()

I want to be able to interpret it even if it's not capitalize or not. If you don't what I'm saying let's say I asked the bot, "how are you?", but the bot was only programmed to answer to that question if it was said like this, "How are you?". I want to simplify the coding so I won't have to use a "or" statement. Please provide example code.

+2  A: 
if talk.upper() == "HOW ARE YOU":
    perform_action()

or, if you prefer to search for substrings

if "HOW ARE YOU" in talk.upper():
    perform_action()
Jim Brissom
+1  A: 

Easy way would be to lowercase everything:

if "how are you" == talk.lower():
cobbal
You should [normalize to uppercase](http://msdn.microsoft.com/en-us/library/bb386042.aspx).
Frédéric Hamidi
@Frédéric Hamidi since they won't be needing to be converted back, I don't see a reason to, although I could be reading it wrong
cobbal
A: 

Use the lower() function.

Previous posting

Mr. Disappointment