您的位置:首页 > 编程语言 > Python开发

python学习之while语句

2016-11-15 16:30 309 查看
while循环

1.简单的while循环
while True:
print("1")
#这是一个简单的while循环,当等于True时会一直打印1

2.while执行多少次后退出

coun=0
while True:
print(coun)
coun+=1
if coun == 10:
print("not while,exit...")
break
#循环10次后退出
运行结果:

0
1
2
3
4
5
6
7
8
9
not while,exit...

3.循环跳过中间某段

coun=0
while True:
coun+=1
if coun >= 4 and coun <= 8:
continue
print(coun)
if coun == 10:
print("not while,exit...")
break
#打印10个数跳过4和8中间的数
运行结果:

1
2
3
9
10
not while,exit...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: