This is a trivial calculator "shell" with a running total. As trivial as it is, I find it to be more useful than a normal calculator when doing my checkbook because of the ever-present running total.
Sed is to Vi as RunningCalc is to Python
python ~/programming/python/hacks/RunningCalc.py $ 0.0+50 $ 50.0-10 $ 40.0*0 $ 0.0+5 $ 5.0-2 $ 3.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/usr/bin/env python
"""This is a trivial calculator "shell" with a running total."""
import sys
current = 0.0
while True:
sys.stdout.write("$ %s" % current)
try:
current = eval(str(current) + raw_input())
except (KeyboardInterrupt, EOFError):
sys.exit(0)
except Exception, e:
sys.stderr.write("Error: %s\n" % e)
|
Tags: programs
A few improvements... Very nice little recipe, but I'm not sure it does what you intended it to (or I misunderstood it). It doesn't actually add (numerically) each new input. I've modified it a bit to correct this, and added a simple clear/quit pair of key commands.
Hmm, yes, my version is clever... I'm sorry, I should have been more clear. Here is an example:
^D (i.e. end of input) exits.
A few improvements..., Tony Ha. I like the print statments, but your version is not a "calculator shell" any more, it dose not take * 10 or / 5 etc..