[Tutor] A little math and Python: rpn - my last one
Gregor Lingl
glingl@aon.at
Thu, 22 Aug 2002 08:57:02 +0200
my last version, this time a practical calculator:
>>> def rpn():
stack=[]
while 1:
item = raw_input("> ")
if item in ["+","-","*","/"]:
res=str(eval(stack[-2]+item+stack[-1]))
print res
stack[-2:]=[res]
elif item == "":
break
else:
stack.append(item)
>>> rpn()
> 3
> 4
> +
7
> 6
> +
13
> 12
> *
156
> 6
> /
26.0
>
>>>
Thanks for your patience
Gregor
P.S.: Do you still think, Danny was right, when he wrote
It might make an interesting project to write an RPN calculator in Python.