您现在的位置是:课程教程文章
你所不知道的python循环中的else
2023-12-18 23:21课程教程文章 人已围观
软件测试功能+自动化+Python+测试开发平台
软件测试功能+自动化+Python+测试开发平台...Python 多线程多进程多协程 并发编程实战
Python 多线程多进程多协程 并发编程实战...Python基础入门Python零基础入门到精通【咕
Python基础入门Python零基础入门到精通【咕咕泡学院VIP系统课程学习计划! 这个课程计划是结合我们的基础VIP课程的深入进...2019Python自动化运维开发【千锋Linux】
2019Python自动化运维开发【千锋Linux】获取视频资料源码,请添加官方指定qq:2377443170 本章内容主要讲解的是计算机...
众多语言中都有if else这对条件选择组合,但是在python中还有更多else使用的地方,比如说循环for,或者while都可以和else组合。
下面简单介绍一下for-else while-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
下一篇:没有了