while eles

作用

当while循环被break打断时,执行后面语句。例如:

count = 0
while count <= 5 :
    count += 1
    if count == 3:break
    print("Loop",count)

else:
    print("循环正常执行完啦")
print("-----out of while loop ------")
View Code

通过break跳出循环,这时也会跳过else语句块,直接执行else之后的语句。因此我们可以在else语句块中写一些不通过break语句跳出时才执行的代码,不用像其他语言,在这种情况下,还要专门写一个判断。

原文地址:https://www.cnblogs.com/zly9527/p/11195604.html