Skip to content Skip to sidebar Skip to footer

While-loop With An Or-condition

I want it to stop once one of the variables gets to the desired number. Why does this code wait until both variables equal 20 or greater to end? z = 20 x = 1 y = 0 while x < z

Solution 1:

If the loop must stop when at least one of the variables is >= z, then you must use and to connect the conditions:

while x < z and y < z:

In your code, by using or you state that as long as one of the variables is < z, the loop must continue - and that's not what you want.

Post a Comment for "While-loop With An Or-condition"