创新互联Python教程: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

python学习网,大量的免费python学习视频,欢迎在线学习!


当前题目:创新互联Python教程:python循环结构中的else怎么使用
文章地址:http://bzwzjz.com/article/dhhdsgj.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 营销型网站建设 移动手机网站制作 网站建设 网站设计公司 网站制作公司 营销型网站建设 成都商城网站制作 高端网站建设 手机网站制作 广安网站设计 成都网站建设 成都网站设计 手机网站制作 公司网站建设 外贸网站建设 成都网站制作 成都网站设计 成都商城网站建设 响应式网站建设 网站设计 成都网站制作 梓潼网站设计