elif command keeps repeating inside while command

时间:2015-07-11 13:18:54

标签: python python-3.x while-loop

I put an elif statement in a while statement, and I expected that when it finishes what's inside it, it'd repeat the while command. What actually happens is the console spams "unknown command" indefinitely. How do I fix this?

while O == 0:
    if bla bla bla
        O = 1
    elif O != 1: #This should happen after it takes all if commands above me into account
        print('unknown command')

2 个答案:

答案 0 :(得分:1)

显然,您的if bla bla bla测试不匹配。

但是,由于O == 0为真,O != 1也是如此,您永远不会更改O的值,从而产生无限循环。无限循环在每次迭代时打印'unknown command',填满你的控制台。

答案 1 :(得分:1)

只要你的O == 0为真,elif语句也是True(o!= 1),除非if语句为True且O更改为1。 所以当执行elif语句时,它会转到infiniteloop,因为在elif中o的值没有改变。或者在elif

中打印后添加break