How to do while loops with multiple conditions


Question

I have a while loop in python

condition1=False
condition1=False
val = -1

while condition1==False and condition2==False and val==-1:
    val,something1,something2 = getstuff()

    if something1==10:
        condition1 = True

    if something2==20:
        condition2 = True

'
'

I want to break out of the loop when all these conditions are true, the code above does not work

I originally had

while True:
      if condition1==True and condition2==True and val!=-1:
         break

which works ok, is this the best way to do this?

Thanks

1
17
1/27/2010 11:37:11 AM

Accepted Answer

Change the ands to ors.

18
1/27/2010 11:28:54 AM

while not condition1 or not condition2 or val == -1:

But there was nothing wrong with your original of using an if inside of a while True.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon