#!/Library/Frameworks/Python.framework/Versions/Current/bin/python # L-22 MCS 275 Wed 3 Mar 2010 : calculate.py import cgi form = cgi.FieldStorage() print "Content-Type: text/html\n" error = False try: x = form['operand1'].value except KeyError: print "please enter first operand" error = True try: y = form['operand2'].value except KeyError: print "please enter second operand" error = True if not error: try: v = eval(form['convert'].value) except: v = False if v: s = str(float(x)) + ' ' s = s + form['operator'].value + ' ' s = s + str(float(y)) else: s = x + ' ' + form['operator'].value + ' ' + y r = eval(s) s = s + ' = ' + str(r) print "" print "

%s

" % s print ""