Original Post
So I wrote this dead simple rpn calculator in python last night. It works just fine but there has to be a better way to map to the various operations instead of all of these elifs. Anybody got any great ideas? I'd love to hear them.
import math
stack = []
print "Type \'halp\' for directions. (\'q\' for quit)"
quitflag = False
while 1:
input = str(raw_input(">>> ")).split()
print input
for token in input:
if token.isdigit():
stack.append(token)
elif token == "pi":
stack.append(math.pi)
elif token == "pop":
stack.pop()
elif token == "p":
print str(stack.pop())
elif token == "+":
stack.append(float(stack.pop())+float(stack.pop()))
elif token == "-":
stack.append(float(stack.pop())-float(stack.pop()))
elif token == "*":
stack.append(float(stack.pop())*float(stack.pop()))
elif token == "/":
stack.append(float(stack.pop())/float(stack.pop()))
elif token == "^":
stack.append(float(stack.pop())**float(stack.pop()))
elif token == "halp":
print "This is a reverse polish notaion (RPN) calculator."
print "Operations are evaluated left to right and results are pushed onto the stack."
print "Example >>> 9 1 2 + ----> [9, 3]"
print "Cont'd >>> + ----> [12]"
print "Cont'd >>> 36 / ----> [3] (division may seem counter intuitive)"
print "Operands: +-*/^ "
print "Commands: pi (push pi); pop (pop stack); p (print)"
elif token == "q":
print "Bai!"
quitflag = True
else:
stack.append(token)
if quitflag == True: # dirty stupid hack
break
print "Stack is:",stack