简体   繁体   English

或条件在while循环中python

[英]or condition in while loop python

Does or condition work in a while loop in python? 在Python中的while循环中是否有效? I can't seem to make it work. 我似乎无法使其工作。 This is the sample of how my code works. 这是我的代码如何工作的示例。

newslot = 3
moved = False

while newslot > 0 or moved != True:
    enabled = query something on the database where slot = newslot
    if enabled:
        print 'do something here'
        moved = True
    else:
        newslot-=1
        print 'slot disabled'

So when the newslot gets to value of zero it still proceeds to go inside the while loop. 因此,当Newslot的值为零时,它仍会进入while循环。 I seem to be missing something here. 我似乎在这里错过了一些东西。

or is working as should be expected. or正在按预期工作。 A while loop will continue until its condition is false. while循环将继续直到其条件为假。 If its condition is two separate conditions connected with an or , it will only be false when the two conditions are both false. 如果其条件是两个单独的条件or ,则只有当两个条件都为假时才为假。

Your loop will continue repeating until moved is false and newslot is <= 0. I'm guessing you actually want to use and in this case, as you want the loop to stop once either condition is met. 您的循环将继续重复,直到moved为false且newslot <=0。我猜您实际上要使用and在这种情况下,因为您希望在满足任一条件时停止循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM