while else

1 count = 0
2 while count <= 5 :
3     count += 1
4     if count == 3:pass
5     print("Loop",count)
6 
7 else:
8     print("循环执行完啦")
9 print("-----out of while loop ------")

结果:

Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环执行完啦
—–out of while loop ——

1 count = 0
2 while count <= 5 :
3     count += 1
4     if count == 3:break
5     print("Loop",count)
6 
7 else:
8     print("循环执行完啦")
9 print("-----out of while loop ------")

 

Loop 1
Loop 2
—–out of while loop ——

结论:while循环正常执行完不会执行else里边的代码,如果while循环被break中断则会执行else里边的代码

    原文作者:waitstory
    原文地址: https://www.cnblogs.com/waitstory/p/10692410.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞