multiple parameters in if statement

John Zenger john_zenger at yahoo.com
Sat Apr 15 23:43:46 EDT 2006


Try this:

if form.get("delete_id","") != "" and form.get("delete_data","") != "" 
and...

the "get" method lets you have an optional second argument that gets 
returned if the key is not in the dictionary.

Also, am I reading your code right?  If I enter some fields but not all, 
you print a message that says "Nothing entered."  Nothing?

The other thing I'd recommend is stick that long list of fields in a 
list, and then do operations on that list:

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

then to see if all those fields are empty:

everything = ""
for field in fields:
     everything += form.get(field,"")
if everything == "":
     print "Absolutely nothing entered!"

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
> 



More information about the Python-list mailing list