having problems with a multi-conditional while statement

B

bowman.joseph

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?
 
P

Philip Semanchuk

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.
 
N

Ned Deily

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:
.... print "never"
....
Unwinding the snippet above:
.... condition2 = True
.... False

# -> while loop exits after 1 trip
 
B

bowman.joseph

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.
 
H

Hendrik van Rooyen

8<------------ nice explanation --------------------
Change the "and" to an "or" and you'll get the result you expected.

Also google for "De Morgan", or "De Morgan's laws"

Almost everybody stumbles over this or one of it's
corollaries at least once in their careers.

- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top