跳出循环
True and False ,当输入1时,a=False时,会执行接下来的语句后再跳出这个循环。
a=True
while a:
b= input('type somesthing')
if b=='1':
a= False
else:
pass
print ('finish run')
''''
type somesthing:2
still in while
type somesthing:3
still in while
type somesthing:1
still in while #会执行下面的语句再跳出
finish run
''''
break
break语句和 C 中的类似,用于跳出最近的 for 或 while 循环。
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
continue语句,也是从C语言借来的,表示继续下一次迭代:
在代码中,满足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:
"""
pass
pass 语句什么也不做。当语法上需要语句但程序不需要动作时,可以使用它。例如:
本文介绍了Python中循环控制的关键概念,包括如何使用while循环配合True、False进行条件判断,利用break、continue和pass语句实现不同的循环行为。通过具体示例展示了这些控制语句的实际应用。
1217

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



