[Tutor] Simple RPN calculator

Bob Gailer bgailer at alum.rpi.edu
Sun Dec 5 11:33:25 CET 2004

At 04:45 PM 12/4/2004, Max Noel wrote:

>On Dec 4, 2004, at 23:30, Alan Gauld wrote:
>>>to make it request for input(s) of say a simple math like  "1 2 3 4
>>5 + - * /".
>>Look at raw_input()
>>But if you are that much of a beginner you need to take several
>>steps back and try one of the tutorials, they all cover raw_input
>>fairly early on...
>>And finally doesn't RPN put the operators first? Or is it me thats
>>getting confused fromtoo much Lisping recently?...
>         Nope, RPN calculators (such as the HP48GX, IMHO the best 
> calculator ever made) require you to input the operands first, then the 
> operators. It's both easier to implement and more intuitive (not to 
> mention way faster to both input and compute) once you've gotten the hang 
> of it.
>         You can probably do a very basic RPN calculator in less than a 
> hundred lines of code, using raw_input() and a stack (well, a list's 
> append() and pop() methods).

For grins I just wrote one that takes '1 2.3 - 3 4 5 + * /' as input and 
prints -0.0481481.... 8 lines of Python. That indeed is less than 100. Took 
about 7 minutes to code and test.

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 

More information about the Tutor mailing list