Calculator

« Home

Recently, I wanted to mess a bit with exceptions in Python, and I came up with this calculator. The point ain't doing a calculator for real, but just use Python libraries and learn a bit from exception handling.

Here's version 0.1 code:

from math import *
import sys
old_help = help
def help(x = ''):
if x:
old_help('math.%s' % x)
else:
old_help('math')
try:
print "\nWelcome to djthyrax's calculator!\n"
print "To see built-in functions, just type help()"
print "Wanna know how to use function NAME?\nJust type help('NAME')"
while 1:
try:
ans = eval(raw_input(">>> "))
except KeyboardInterrupt:
print
except EOFError:
raise EOFError
except:
print "Error: %s" % str(sys.exc_info()[1])
else:
if type(ans) in [type(1), type(1.0)]:
print ans
else:
print
except EOFError:
print
except:
print "Woops!"

You can discuss this code (in portuguese) here: Calculadora 

I hold no rights in this code, feel free to use it for whatever you want.