import sys, cgi
sys.stderr = sys.stdout
import MySQLdb
import cgitb; cgitb.enable()
print "Content-Type: text/html\n\n"
print "<html>"
print "<head><title>Check Answer</title></head>"
print "<body>"
print '<table width="500" border="0" align="center"><tr><td>'
form = cgi.FieldStorage()
conn = MySQLdb.connect(user="xxx",passwd="xxx")
conn.select_db("usa")
curr = conn.cursor()
numrecs = curr.execute("select capital, statename, postal from states;")
recs = curr.fetchall()
answers = {}
for record in recs:
city, statename, postal = record
answers[postal] = (city, statename)
chosenstate = form["thestate"].value
rightanswer = answers[chosenstate][0]
if form["capitals"].value == rightanswer:
print "<h1>You got it right!</hi>"
else:
print "<h1>No, sorry<h1>"
print "<p>The capital of %s is %s</p>" % (answers[chosenstate][1], rightanswer)
print """
<form name="quiz" action="form1.cgi" method="GET">
<input type="submit" value="Again!">
</form>
"""
print "</td></tr></table>"
print "</body>"
print "</html>"
# code highlighted using py2html.py version 0.8