导语:
本文主要介绍了关于python循环结构中的else怎么使用的相关知识,希望可以帮到处于编程学习途中的小伙伴
当执行循环组合中的else时,循环正常结束(即不使用break退出)。如以下代码:
numbers= [1,2,3,4,5]
for nin numbers:
if (n >5):
print('the value is %d '%(n))
break
else:
print('the for loop does not end with break')
i= 0
while(numbers[i] <5):
print('the index %d value is %d'%(i, numbers[i]))
if (numbers[i] <0) :
break
i= i+ 1
else:
print('the loop does not end with break')
numbers= [1,2,3,4,5]
for nin numbers:
if (n >5):
print('the value is %d '%(n))
break
else:
print('the for loop does not end with break')
i= 0
while(numbers[i] <5):
print('the index %d value is %d'%(i, numbers[i]))
if (numbers[i] <0) :
break
i= i+ 1
else:
print('the loop does not end with break')
执行结果如下:
C:\Python27>python.exe for_else.py
thefor loop doesnot end withbreak
the index0 valueis 1
the index1 valueis 2
the index2 valueis 3
the index3 valueis 4
the loop doesnot end withbreak
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何用eclipse打开python项目10/12
- ♥ python中globals()和locals()函数的使用12/22
- ♥ Python判断是否为目录09/25
- ♥ 为什么python如此受欢迎09/23
- ♥ 如何对 Python 列表进行重复数据删除08/27
- ♥ python pyg2plot的原理10/02
内容反馈