Skip to content

Instantly share code, notes, and snippets.

Created June 28, 2013 02:58
Show Gist options
  • Save anonymous/5882172 to your computer and use it in GitHub Desktop.
Save anonymous/5882172 to your computer and use it in GitHub Desktop.
operations = {
"+": lambda x, y: x + y,
"-": lambda x, y: x - y,
"/": lambda x, y: x / y,
"*": lambda x, y: x * y
}
def calculate(expr):
numxChars = ""
operation = None
numyChars = ""
for char in expr:
if char.isdigit():
if operation is None:
numxChars += char
else:
numyChars += char
elif char in operations:
operation = char
elif char.isspace:
pass
else:
raise Exception("invalid charecter: " + char)
return operations[operation](int(numxChars), int(numyChars))
print calculate(raw_input("Input? "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment