having problems with a multi-conditional while statement

2009-01-06 Thread bowman.jos...@gmail.com
Hi,

I'm trying to write a multi-conditional while statement, and am having
problems. I've broken it down to this simple demo.

#!/usr/bin/python2.5

condition1 = False
condition2 = False

while not condition1 and not condition2:
print 'conditions met'
if condition1:
condition2 = True
condition1 = True


As I understand it, this should print 'conditions met' twice, however,
it only prints it once. It seems that once condition1 is made true,
the whole thing evaluates as true and stops the while loop.

I've also tried to set the while condition the following ways also,
and had the same problem

while (not condition1 and not condition2):
while (not condition1) and (not condition2):
while condition1 != True and condition2 != True:
while (condition1 != True and condition2 != True):
while (condition1 != True) and (condition2 != True):

Can someone lend me a hand in understanding this?

--
http://mail.python.org/mailman/listinfo/python-list


Re: having problems with a multi-conditional while statement

2009-01-06 Thread bowman.jos...@gmail.com
Thanks for the assistance. I actually realized I was making things
more complicated than they needed to be and I really only needed one
condition to be met.

On Jan 6, 7:42 pm, Ned Deily  wrote:
> In article
> <40a44d6b-c638-464d-b166-ef66496a0...@l16g2000yqo.googlegroups.com>,
>
>
>
>  "bowman.jos...@gmail.com"  wrote:
> > Hi,
>
> > I'm trying to write a multi-conditional while statement, and am having
> > problems. I've broken it down to this simple demo.
>
> > #!/usr/bin/python2.5
>
> > condition1 = False
> > condition2 = False
>
> > while not condition1 and not condition2:
> >     print 'conditions met'
> >     if condition1:
> >         condition2 = True
> >     condition1 = True
>
> > As I understand it, this should print 'conditions met' twice, however,
> > it only prints it once. It seems that once condition1 is made true,
> > the whole thing evaluates as true and stops the while loop.
>
> Are you perhaps expecting that the "while" condition is tested at the
> end of the loop?   It's not; it is tested at the top of the loop, so,
> once the condition evaluates as false, the loop exits.  This can even
> result in zero trips:
>
> >>> while False:
>
> ...     print "never"
> ...
>
>
>
> Unwinding the snippet above:
>
> >>> condition1 = False
> >>> condition2 = False
> >>> not condition1 and not condition2
> True
> >>> if condition1:
>
> ...     condition2 = True
> ...>>> condition1 = True
> >>> not condition1 and not condition2
>
> False
>
> # -> while loop exits after 1 trip
>
> --
>  Ned Deily,
>  n...@acm.org

--
http://mail.python.org/mailman/listinfo/python-list