1

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.

cobbal
  • 69,903
  • 20
  • 143
  • 156
Noah R
  • 5,287
  • 21
  • 56
  • 75
  • Is this homework? try looking at `talk.lower()` – John La Rooy Oct 09 '10 at 21:54
  • It's really important to note that the code you wrote above, as written, will not work as you expect it to. It will *always* evaluate to True, and perform_action() will always be called. The conditional can be parsed as [if ("how are you") or ("How are you" in talk):], which always returns true, because "how are you" evaluates to true. What you want is probably [if "how are you" in talk or "How are you" in talk:] – JohnFilleau Aug 07 '14 at 19:02

2 Answers2

2
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
  • 31,821
  • 4
  • 39
  • 33
1

Easy way would be to lowercase everything:

if "how are you" == talk.lower():
cobbal
  • 69,903
  • 20
  • 143
  • 156