循环组合中的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
python学习网,大量的免费python学习视频,欢迎在线学习!
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!