Created
June 28, 2013 02:58
-
-
Save anonymous/5882172 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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