While 循环
For 循环 | While 循环 |
---|---|
遍历一组对象 | 条件为false时自动终止 |
没有break也可以结束 | 使用break语句才能退出循环 |
count = 0
while count <= 5:
print(count)
count = count + 1
#输出:
0
1
2
3
4
5
break语句
使用break可以完全退出循环
num = 0
while True:
if num == 2:
print('Found 2')
break
num += 1
print (num)
#输出:
1
2
Found 2
TASK:
0~5列表逐渐移除
candidates = list(range(0, 5))
re=[]
while len(candidates)>0:
print(candidates)
re=candidates[0]
candidates.remove(re)
#输出:
[0, 1, 2, 3, 4]
[1, 2, 3, 4]
[2, 3, 4]
[3, 4]
[4]