#!/usr/bin/python2.2 import sys, cgi sys.stderr = sys.stdout import MySQLdb import cgitb; cgitb.enable() print "Content-Type: text/html\n\n" print "" print "Check Answer" print "" print '
' form = cgi.FieldStorage() conn = MySQLdb.connect(user="xxx",passwd="xxx") # actual values concealed 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 "

You got it right!" else: print "

No, sorry

" print "

The capital of %s is %s

" % (answers[chosenstate][1], rightanswer) print """
""" print "

" print "" print ""