while循环python中的or条件

2024-05-18 17:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

或者条件在python中的while循环中有效吗?我好像做不到。这是我的代码如何工作的示例。在

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'

所以当newslot的值为0时,它仍然继续进入while循环。 我好像少了点什么。在


Tags: or代码falsetrue示例onenabled条件
1条回答
网友
1楼 · 发布于 2024-05-18 17:42:24

or工作正常。while循环将继续,直到其条件为false。如果它的条件是与or相连的两个独立条件,则只有当这两个条件都为false时,它才会为false。在

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

相关问题 更多 >

    热门问题