或 while 循环 python 中的条件 [英] or condition in while loop python

查看:42
本文介绍了或 while 循环 python 中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

or condition 在 python 的 while 循环中是否有效?我似乎无法让它发挥作用.这是我的代码如何工作的示例.

Does or condition work in a while loop in python? 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'

因此,当新闻槽的值为零时,它仍会继续进入 while 循环.我似乎在这里遗漏了一些东西.

So when the newslot gets to value of zero it still proceeds to go inside the while loop. I seem to be missing something here.

推荐答案

or 正常工作.while 循环将继续直到其条件为假.如果它的条件是用连接的两个独立条件,则只有当两个条件都为假时,它才会为假.

or is working as should be expected. A while loop will continue until its condition is false. If its condition is two separate conditions connected with an or, it will only be false when the two conditions are both false.

您的循环将继续重复,直到 moved 为 false 且 newslot 为 <= 0.我猜您实际上想使用 在这种情况下,因为您希望循环在满足任一条件后停止.

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.

这篇关于或 while 循环 python 中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆