Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > multiple parameters in if statement...

Reply
Thread Tools

multiple parameters in if statement...

 
 
Kun
Guest
Posts: n/a
 
      04-16-2006
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
 
Reply With Quote
 
 
 
 
mensanator@aol.com
Guest
Posts: n/a
 
      04-16-2006

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


 
Reply With Quote
 
 
 
 
Kun
Guest
Posts: n/a
 
      04-16-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-16-2006
Kun wrote:

> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM
TemplateColumn with multiple parameters Maziar Aflatoun ASP .Net 1 01-03-2004 05:57 PM



Advertisments