Decision (if, else) routine is not working as intended with CGImodule

Discussion in 'Python' started by epsilon, Jan 29, 2008.

  1. epsilon

    epsilon Guest

    All:

    I'm running into trouble figuring this one out. It seems that my
    decision routine is not working as intended. Does anyone know why my
    output continues to utilize the "else" portion of the routine.

    Thank you,
    Christopher

    ++++++++++

    #!/usr/bin/python

    import cgi

    print "Content-type: text/plain\n"
    tag_form = cgi.FieldStorage(keep_blank_values=True)

    #if not tag_form.has_key("fse00"):
    if tag_form["fse00"] == "":
    fse000 = {"fse00": "0"}
    tag_form.update(fse000)
    print "Printing fse000: ", tag_form["fse00"]
    else:
    print "Printing fse00: ", tag_form["fse00"]
     
    epsilon, Jan 29, 2008
    #1
    1. Advertising

  2. Re: Decision (if,else) routine is not working as intended with CGI module

    En Tue, 29 Jan 2008 18:23:41 -0200, epsilon <> escribi�:

    > I'm running into trouble figuring this one out. It seems that my
    > decision routine is not working as intended. Does anyone know why my
    > output continues to utilize the "else" portion of the routine.
    >
    > tag_form = cgi.FieldStorage(keep_blank_values=True)
    >
    > #if not tag_form.has_key("fse00"):
    > if tag_form["fse00"] == "":


    tag_form["fse00"] is a FieldStorage instance, not a string. To get its
    value, use:

    if tag_form["fse00"].value == ""
    if tag_form.getvalue("fse00")==""
    if tag_form.getfirst("fse00")==""

    See http://docs.python.org/lib/module-cgi.html

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 29, 2008
    #2
    1. Advertising

  3. epsilon

    Guest

    On 29 jan, 21:23, epsilon <> wrote:
    > All:
    >
    > I'm running into trouble figuring this one out. It seems that my
    > decision routine is not working as intended. Does anyone know why my
    > output continues to utilize the "else" portion of the routine.


    Probably because the test expression 'tag_form["fse00"] == ""' evals
    to false ?

    Hint: try printing out tag_form["fse00"] before the test, ie:


    (snip)
    >
    > #!/usr/bin/python
    >
    > import cgi
    >
    > print "Content-type: text/plain\n"
    > tag_form = cgi.FieldStorage(keep_blank_values=True)
    >


    print "tag_form[\"fse00\"] is actually: %s" % tag_form["fse00"]
    print "tag_form[\"fse00\"] == '' evals to: %s" % (tag_form["fse00"]
    == '')

    > if tag_form["fse00"] == "":
    > fse000 = {"fse00": "0"}
    > tag_form.update(fse000)
    > print "Printing fse000: ", tag_form["fse00"]
    > else:
    > print "Printing fse00: ", tag_form["fse00"]


    HTH
     
    , Jan 29, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maney
    Replies:
    1
    Views:
    311
    Gabriel Genellina
    Apr 11, 2007
  2. Homer J. Simpson
    Replies:
    0
    Views:
    422
    Homer J. Simpson
    Aug 29, 2007
  3. Basildk
    Replies:
    8
    Views:
    628
    Patrice
    Aug 31, 2007
  4. Mike
    Replies:
    4
    Views:
    220
  5. chaitu
    Replies:
    2
    Views:
    166
    chaitu
    Sep 21, 2006
Loading...

Share This Page