创新互联Python教程:python循环结构中的else怎么使用

疆括仕网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

创新互联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怎么使用
转载来源:https://tyhkzb.com/article/dhhdsgj.html
在线咨询
服务热线
服务热线:028-86922220
TOP