Python if语句在for循环中

这是我的一部分代码:

for unit in range(7010, 7016):
    print unit 
    text = (dev.cli("show " + str(unit) , warning=False))
    if "word" in text:
        print 'ok'
    unit += 1

当我运行这段代码时,我得到如下输出:

7010
7011
7012
7013
7014
7015
ok

因此,IF语句仅在最后一个for循环中执行。 有人可以解释我为什么吗?

评论
  • ncum
    ncum 回复

    Probably you see this effect because of use dev.cli("show "...) to print unit in the loop. Probably it sends data directly to the console. print send data to buffer and show output on the buffer flush (at the end of the program)