# Python While ... Else **Published by:** [visbkf](https://paragraph.com/@8585882/) **Published on:** 2023-03-13 **URL:** https://paragraph.com/@8585882/python-while-else ## Content 是的,在Python中,while 循环可以有一个可选的 else 块,在循环完成其迭代后执行,但仅当循环正常结束时(即没有break语句)。 以下是一个带有 else 块的 while 循环的示例:Copy codei = 0 while i < 5: print(i) i += 1 else: print("循环完成且没有使用break语句") 在这个示例中,循环从i == 0迭代到 i == 4,打印每个 i 的值。循环完成后,执行 else 块,打印消息“循环完成且没有使用break语句”。 请注意,如果在while循环中遇到 break语句,将不会执行else块。 ## Publication Information - [visbkf](https://paragraph.com/@8585882/): Publication homepage - [All Posts](https://paragraph.com/@8585882/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@8585882): Subscribe to updates