Created
March 10, 2010 14:28
-
-
Save sma/327910 to your computer and use it in GitHub Desktop.
upn calculator
This file contains hidden or 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
| from operator import add, sub, mul, truediv as div | |
| operations = {"+": add, "-": sub, "*": mul, "/": div} | |
| stack = [] | |
| def upn(line): | |
| for token in line.split(): | |
| if token in operations: | |
| b, a = stack.pop(), stack.pop() | |
| stack.append(operations[token](a, b)) | |
| else: | |
| stack.append(float(token)) | |
| print(round(stack[-1], 4)) | |
| while True: | |
| try: upn(input("> ")) | |
| except EOFError: break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment