having problems with a multi-conditional while statement

Philip Semanchuk philip at semanchuk.com
Wed Jan 7 01:34:32 CET 2009


On Jan 6, 2009, at 7:18 PM, bowman.joseph at 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.


Think it through.

At the outset:
while (not condition1) and (not condition2) ==>
while (not False) and (not False) ==>
while True and True ==>
while True

After it's been through the loop once:
while (not condition1) and (not condition2) ==>
while (not True) and (not False) ==>
while False and True ==>
while False


Change the "and" to an "or" and you'll get the result you expected.







More information about the Python-list mailing list