while-else

Posted ccqc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了while-else相关的知识,希望对你有一定的参考价值。

while-else

1、break立即打断循环并且不再执行else

count = 0
while count <= 5:
    count += 1
    if count == 3:
        break
    print("loop",count)
else:
    print("循环执行完了")
print("end")

技术分享图片

 

2、while循环后会执行else

count = 0
while count <= 5:
    count += 1
    if count == 3:
        pass
    print("loop",count)
else:
    print("循环执行完了")
print("end")

 技术分享图片

 

以上是关于while-else的主要内容,如果未能解决你的问题,请参考以下文章