multiple parameters in if statement...

Discussion in 'Python' started by Kun, Apr 16, 2006.

  1. Kun

    Kun Guest

    I am trying to make an if-statement that will not do anything and print
    'nothing entered' if there is nothing entered in a form. I have the
    following code that does that, however, now even if I enter something
    into the form, the code still outputs 'nothing entered'. This violates
    the if statement and I am wondering what I did wrong.

    if form.has_key("delete_id") and form["delete_id"].value != "" and
    form.has_key("delete_date") and form["delete_date"].value != "" and
    form.has_key("delete_purchasetype") and
    form["delete_purchasetype"].value != "" and form.has_key("delete_price")
    and form["delete_price"].value != "" and form.has_key("delete_comment")
    and form["delete_comment"].value != "":
    delete_id=form['delete_id'].value
    delete_date=form['delete_date'].value
    delete_purchasetype=form['delete_purchasetype'].value
    delete_price=form['delete_price'].value
    delete_comment=form['delete_comment'].value
    else:
    print "ERROR: Nothing entered!"
    raise Exception
     
    Kun, Apr 16, 2006
    #1
    1. Advertising

  2. Kun

    Guest

    Kun wrote:
    > I am trying to make an if-statement that will not do anything and print
    > 'nothing entered' if there is nothing entered in a form. I have the
    > following code that does that, however, now even if I enter something


    Yes, but did you enter everything?

    > into the form, the code still outputs 'nothing entered'.


    The logic doesn't imply "nothing", it implies "not everything".
    The else clause will execute if ANY item is not enetered.

    > This violates
    > the if statement and I am wondering what I did wrong.
    >
    > if form.has_key("delete_id") and form["delete_id"].value != "" and
    > form.has_key("delete_date") and form["delete_date"].value != "" and
    > form.has_key("delete_purchasetype") and
    > form["delete_purchasetype"].value != "" and form.has_key("delete_price")
    > and form["delete_price"].value != "" and form.has_key("delete_comment")
    > and form["delete_comment"].value != "":
    > delete_id=form['delete_id'].value
    > delete_date=form['delete_date'].value
    > delete_purchasetype=form['delete_purchasetype'].value
    > delete_price=form['delete_price'].value
    > delete_comment=form['delete_comment'].value
    > else:
    > print "ERROR: Nothing entered!"
    > raise Exception
     
    , Apr 16, 2006
    #2
    1. Advertising

  3. Kun

    Kun Guest

    wrote:
    > Kun wrote:
    >> I am trying to make an if-statement that will not do anything and print
    >> 'nothing entered' if there is nothing entered in a form. I have the
    >> following code that does that, however, now even if I enter something

    >
    > Yes, but did you enter everything?
    >
    >> into the form, the code still outputs 'nothing entered'.

    >
    > The logic doesn't imply "nothing", it implies "not everything".
    > The else clause will execute if ANY item is not enetered.
    >
    >> This violates
    >> the if statement and I am wondering what I did wrong.
    >>
    >> if form.has_key("delete_id") and form["delete_id"].value != "" and
    >> form.has_key("delete_date") and form["delete_date"].value != "" and
    >> form.has_key("delete_purchasetype") and
    >> form["delete_purchasetype"].value != "" and form.has_key("delete_price")
    >> and form["delete_price"].value != "" and form.has_key("delete_comment")
    >> and form["delete_comment"].value != "":
    >> delete_id=form['delete_id'].value
    >> delete_date=form['delete_date'].value
    >> delete_purchasetype=form['delete_purchasetype'].value
    >> delete_price=form['delete_price'].value
    >> delete_comment=form['delete_comment'].value
    >> else:
    >> print "ERROR: Nothing entered!"
    >> raise Exception

    >

    How do I make this so that it only prints 'nothing entered' when none of
    the fields are entered?
     
    Kun, Apr 16, 2006
    #3
  4. Kun

    Peter Otten Guest

    Kun wrote:

    > wrote:
    >> Kun wrote:
    >>> I am trying to make an if-statement that will not do anything and print
    >>> 'nothing entered' if there is nothing entered in a form. I have the
    >>> following code that does that, however, now even if I enter something

    >>
    >> Yes, but did you enter everything?
    >>
    >>> into the form, the code still outputs 'nothing entered'.

    >>
    >> The logic doesn't imply "nothing", it implies "not everything".
    >> The else clause will execute if ANY item is not enetered.
    >>
    >>> This violates
    >>> the if statement and I am wondering what I did wrong.
    >>>
    >>> if form.has_key("delete_id") and form["delete_id"].value != "" and
    >>> form.has_key("delete_date") and form["delete_date"].value != "" and
    >>> form.has_key("delete_purchasetype") and
    >>> form["delete_purchasetype"].value != "" and form.has_key("delete_price")
    >>> and form["delete_price"].value != "" and form.has_key("delete_comment")
    >>> and form["delete_comment"].value != "":
    >>> delete_id=form['delete_id'].value
    >>> delete_date=form['delete_date'].value
    >>> delete_purchasetype=form['delete_purchasetype'].value
    >>> delete_price=form['delete_price'].value
    >>> delete_comment=form['delete_comment'].value
    >>> else:
    >>> print "ERROR: Nothing entered!"
    >>> raise Exception

    >>

    > How do I make this so that it only prints 'nothing entered' when none of
    > the fields are entered?


    def has_data(form, fields):
    for field in fields:
    if form.has_key(field) and form[field] != "":
    return True
    return False

    fields = ["delete_id", "delete_date", "delete_purchasetype", "delete_price",
    "delete_comment"]

    if not has_data(form, fields):
    print "nothing entered"

    Just testing for

    if field in form: ...

    instead of

    if form.has_key(field) and form[field] != "": ...

    is probably sufficient if form is a cgi.FieldStorage.

    Peter
     
    Peter Otten, Apr 16, 2006
    #4
    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. Replies:
    3
    Views:
    837
  2. Jay McGavren
    Replies:
    11
    Views:
    1,186
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,166
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    497
    Duncan Booth
    Jul 22, 2004
  5. Kun
    Replies:
    6
    Views:
    402
    John Machin
    Apr 16, 2006
Loading...

Share This Page