或者 while 循环 python 中的条件

3 python while-loop

or 条件在 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'
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*nTG 5

or正在按预期工作。循环while将继续下去,直到条件为假为止。如果它的条件是用 an 连接的两个单独的条件or,则只有当两个条件都为假时它才会为假。

你的循环将继续重复,直到movedis false 并且newslotis <= 0。我猜你实际上想在这种情况下使用and,因为你希望循环在满足任一条件时停止。