469,923 Members | 1,298 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

multiple parameters in if statement...

Kun
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
Apr 16 '06 #1
3 1697

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 '06 #2
Kun
me********@aol.com 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?
Apr 16 '06 #3
Kun wrote:
me********@aol.com 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

Apr 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by tshad | last post: by
12 posts views Thread by Dennis D. | last post: by
7 posts views Thread by Ceebaby via AccessMonster.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.