[Tutor] CGI Calculator

Darren Williams D3IBZ at hotmail.com
Mon Jul 16 19:52:46 CEST 2007


Ok, now i've modified my script but am getting another error, i've commented 
a few useless (hopefully) lines out -

#!/usr/bin/env python

import cgitb; cgitb.enable()
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
    coatSize = form["coatSize"].value
    usedPockets = form["usedPockets"].value
    labSpace = form["labSpace"].value
    totalJunkies = form["totalJunkies"].value
    DVR = form["DVR"].value

    while usedPockets > totalJunkies - labSpace * 17:
        Tokens = Tokens + 1
        usedPockets = (usedPockets - totalJunkies + labSpace) * 17
        totalJunkies = totalJunkies + 1
        print "Tokens"
#    else:
#        print "Try again"

main()

Here's the error using the CGI Traceback Module -

  D:\inetpub\vhosts\newspyn.com\cgi-bin\JunkieCalc.py
         21         print "Tokens"

         22 #    else:

         23 #        print "Try again... beeyatch"

         24

         25 main()

      main = <function main>
       D:\inetpub\vhosts\newspyn.com\cgi-bin\JunkieCalc.py in main()
         15     DVR = form["DVR"].value

         16

         17     while usedPockets > totalJunkies - labSpace * 17:

         18         Tokens = Tokens + 1

         19         usedPockets = (usedPockets - totalJunkies + labSpace) * 
17

      usedPockets = '192000', totalJunkies = '200', labSpace = '0'

TypeError: unsupported operand type(s) for -: 'str' and 'str'
      args = ("unsupported operand type(s) for -: 'str' and 'str'",)



It's confused me - it says I can't subtract a string from a string but then 
gives the value's of the variables (that I randomly entered into the form) 
at the bottom - usedPockets = '192000', totalJunkies = '200', labSpace = '0'

Thanks in advance for any help :)

----- Original Message ----- 
From: "Eric Brunson" <brunson at brunson.com>
To: "Darren Williams" <D3IBZ at hotmail.com>
Cc: <tutor at python.org>
Sent: Monday, July 16, 2007 12:50 PM
Subject: Re: [Tutor] CGI Calculator


> 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>&#32;<input type="text" name="coatSize">
>>         <p><b>Used Pockets:</b>&#32;<input type="text" 
>> name="usedPockets">
>>         <p><b>Lab Space:</b>&#32;<input type="text" name="labSpace">
>>         <p><b>Total Junkies:</b>&#32;<input type="text" 
>> name="totalJunkies">
>>         <p><b>Dealer Visits Remaining:</b>&#32;<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 at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
> 



More information about the Tutor mailing list