跳出循环
True and False ,当输入1时,a=False时,会执行接下来的语句后再跳出这个循环。
# -*- coding:utf-8 -*-
a=True
while a:
b= input('type somesthing')
if b==1:
a= False
else:
print('none')
print ('finish run')
‘‘’’’
type somesthing2
type somesthing2
type somesthing1
finish run
‘‘’’’
break
break用法,在循环语句中,使用 break, 当符合跳出条件时,会直接结束循环,这是 break 和 True False 的区别。
while True:
b= input('type somesthing:')
if b==1:
break
else:
pass
print('still in while')
print ('finish run')
"""
type somesthing:4
still in while
type somesthing:5
still in while
type somesthing:1
finish run
"""
continue
在代码中,满足b=1的条件时,因为使用了 continue , python 不会执行 else 后面的代码,而会直接进入下一次循环。
while True:
b=input('input somesthing:')
if b==1:
continue
else:
pass
print('still in while' )
print ('finish run')
"""
input somesthing:3
still in while
input somesthing:1 # 没有"still in while"。直接进入下一次循环
input somesthing:4
still in while
input somesthing:
"""
本文深入探讨了Python中循环控制的三种关键方式:使用True和False条件控制循环的流程,利用break语句立即终止循环,以及通过continue跳过当前迭代进入下一次循环。通过具体示例,读者可以详细了解每种控制方式的运作机制。
7万+

被折叠的 条评论
为什么被折叠?



