Darren Williams wrote: > Hi all, > > I am a Python convert coming from a JavaScript background
Welcome to Python, Darren. > (as you can probably tell) and am currently writing my first > application using Python which will be a calculator for an online game > I used to play (thought it would be a decent first project) but am not > sure on the syntax for referencing an HTML form input field, I tried > this (which returns an error) - > > XHTML form - > > <?xml version="1.0" encoding="UTF-8"?> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> > <head> > <title>DopeWars Junkie Calculator</title> > </head> > <body> > <center> > <form method="post" action="http://newspyn.com/cgi-bin/JunkieCalc.py"> > <p><b>Coat Size:</b> <input type="text" name="coatSize"> > <p><b>Used Pockets:</b> <input type="text" name="usedPockets"> > <p><b>Lab Space:</b> <input type="text" name="labSpace"> > <p><b>Total Junkies:</b> <input type="text" > name="totalJunkies"> > <p><b>Dealer Visits Remaining:</b> <input type="text" > name="DVR"> > <p><input type="submit" value="Submit"> > </form> > </body> > </html> > > junkieCalc.py - > > #!/usr/bin/env python > > import cgi > > def main(): > print "Content-type: text/html\n" > form = cgi.FieldStorage() > if form.has_key("coatSize") and form.has_key("usedPockets") and > form.has_key("labSpace") and form.has_key("totalJunkies") and > form.has_key("DVR") and form["coatSize"].value != "" and > form["usedPockets"].value != "" and form["labSpace"].value != "" and > form["totalJunkies"].value != "" and form["DVR"].value != "": > Tokens = 0 > while usedPockets > (totalJunkies - labSpace) * 17: > Tokens = Tokens + 1 > usedPockets = (usedPockets - totalJunkies + labSpace) * 17 > totalJunkies = totalJunkies + 1 > print "Tokens" > else: > print "Try again" > > main() > > This is the error i'm getting - > > Traceback (most recent call last): File > "D:\inetpub\vhosts\newspyn.com\cgi-bin\JunkieCalc.py", line 23, in ? > main() File "D:\inetpub\vhosts\newspyn.com\cgi-bin\JunkieCalc.py", > line 10, in main while usedPockets > (totalJunkies - labSpace) * 17: > UnboundLocalError: local variable 'usedPockets' referenced before > assignment So, you get an error on line 10, which is: while usedPockets > (totalJunkies - labSpace) * 17: and it says that your variable is referenced before assignment. Have you assigned a value to it? In your intro you ask how to reference an HTML form field, well you're already doing it in your if statement: form["labSpace"].value, so if you want a local variable named usedPockets, you should assign a value to it, like: usedPockets = form["usedPockets"].value As an aside, a nice tool for helping debug CGI scripts is the CGI Traceback module. Add this as the first line of the program (after the shebang line, before the import cgi: import cgitb; cgitb.enable() Hope that helps, e. > > Thanks in advance for any help :) > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor