結果: Loop 1Loop 2Loop 3Loop 4Loop 5Loop 6迴圈執行完啦 out of while loop Loop 1Loop 2 out of while loop 結論:while迴圈正常執行完不會執行else裡邊的代碼,如果while迴圈被break中斷則會執行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裡邊的代碼