![图片[1]-python循环结构中的else怎么使用-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5e8703373d3ac649.jpg)
循环组合中的else执行的情况下是循环正常结束(即不是使用break退出)。如下列代码:
numbers=[1,2,3,4,5]
forninnumbers:
if(n>5):
print('thevalueis%d'%(n))
break
else:
print('theforloopdoesnotendwithbreak')
i=0
while(numbers[i]<5):
print('theindex%dvalueis%d'%(i,numbers[i]))
if(numbers[i]<0):
break
i=i+1
else:
print('theloopdoesnotendwithbreak')
numbers=[1,2,3,4,5]
forninnumbers:
if(n>5):
print('thevalueis%d'%(n))
break
else:
print('theforloopdoesnotendwithbreak')
i=0
while(numbers[i]<5):
print('theindex%dvalueis%d'%(i,numbers[i]))
if(numbers[i]<0):
break
i=i+1
else:
print('theloopdoesnotendwithbreak')
执行结果如下:
C:\Python27>python.exefor_else.py theforloopdoesnotendwithbreak theindex0valueis1 theindex1valueis2 theindex2valueis3 theindex3valueis4 theloopdoesnotendwithbreak原文来自:https://www.py.cn
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















































暂无评论内容