#!/Library/Frameworks/Python.framework/Versions/Current/bin/python
# L-19 MCS 275 Wed 27 Feb 2008 : 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 "<html><body>"
   print "<h1>%s</h1>" % s
   print "</body></html>"
