3

I apologize if I didn't explain this well enough but I want to know how I could use my console in my app to take an input like "add 5 4" so it would actually add 5+4 instead of just printing it out. I really just need a function that could take the string "add 5 4" and recognize i started with the word add.

darkdoughnut
  • 47
  • 1
  • 5

1 Answers1

1

If you want to make a fully-fledged interpreter, I would say learn pyParsing.

Otherwise,

def parse(string):
    words = string.rsplit()
    if words[0] == "add":
        print int(word[1]) + int(word[2])

parse(raw_input());

Notice that I'm doing absolutely no error checking, you should have that in your app.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
  • 1
    I would also suggest this (if you are really serious about writing something large): http://stackoverflow.com/questions/1669/learning-to-write-a-compiler – Dhaivat Pandya May 21 '11 at 17:33