The complete improved CGI script

#!/usr/local/bin/python
import cgi, math
print "Content-type: text/html\n" # std opening

# extract the value of the variable "r":
form = cgi.FieldStorage()
# r is not available the first time we run the script:
if form.has_key("r"):  # 
    r = form.getvalue("r")
    s = str(math.sin(float(r)))
else:
    r = 1.2  # default
    s = ""

# print complete form with value:
print """
<HTML><BODY BGCOLOR="white">
<FORM ACTION="hw2.py.cgi" METHOD="POST">
Hello, World! The sine of 
<INPUT TYPE="text" NAME="r" SIZE="10" VALUE="%s">
<INPUT TYPE="submit" VALUE="equals" NAME="equalsbutton">
%s </FORM></BODY></HTML>\n""" % (r,s)

previousnexttable of contents