multiple parameters in if statement

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Apr 15 22:06:26 EDT 2006


On Sat, 15 Apr 2006 20:28:47 -0400, 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 
> 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


That's rather, um, unfortunate looking code.

Instead of making lots of tests like this:

if form.has_key(key) and form[key].value != "" ...

you might find it useful to create a helper function:

def get(form, key):
    if form.has_key(key):
        return form[key].value
    else:
        return ""

Now you don't need to test for existence and non-emptiness, because
missing values will be empty. You just use it like this:

if get(form, key) != "" and ...


Using the get helper function, your test becomes much simpler:

if get(form, "delete_id") != "" and get(form, "delete_date") != "" \
and get(form, "delete_purchasetype") != "" and \
get(form, "delete_price") != "" and get(form, "delete_comment") != "":
    do_something()
else:
    raise ValueError("nothing entered")

But that's still too complicated. In Python, every object can be
tested by if...else directly. Strings are all True, except for the empty
string, which is False.

As an experiment, try this:

if "something":
    print "Something is not nothing."
else:
    print "Empty string."

if "":
    print "Something is not nothing"
else:
    print "Empty string."


So your if...else test becomes simpler:

if get(form, "delete_id") and get(form, "delete_date") and \
get(form, "delete_purchasetype") and get(form, "delete_price") and \
get(form, "delete_comment"):
    do_something()
else:
    raise ValueError("nothing entered")


Now your test checks that every field is non-empty, and if so, calls
do_something(), otherwise it raises an error.

But in fact, that gets the logic backwards. You want to raise an error
only if every field is empty. So your test becomes:

if get(form, "delete_id") or get(form, "delete_date") or \
get(form, "delete_purchasetype") or get(form, "delete_price") or \
get(form, "delete_comment"):
    do_something()
else:
    raise ValueError("nothing entered")




-- 
Steven.




More information about the Python-list mailing list