Newer
Older
def calculate(arg):
stack = []
tokens = arg.split(" ")
for token in tokens:
try:
stack.append(int(token))
except ValueError:
val2 = stack.pop()
if token == '+':
result = val1 + val2
elif token == '-':
if len(stack) > 1:
raise ValueError("too many arguments on the stack")