Skip to content

Instantly share code, notes, and snippets.

@sma
Created March 10, 2010 14:28
Show Gist options
  • Select an option

  • Save sma/327910 to your computer and use it in GitHub Desktop.

Select an option

Save sma/327910 to your computer and use it in GitHub Desktop.
upn calculator
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