下面简单介绍一下for-else while-else组合
循环组合中的else执行的情况下是循环正常结束(即不是使用break退出)。如下列代码:
numbers = [1,2,3,4,5] for n in 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 value is %d '%(n)) break else: print('the for loop does not end with break') i = 0 while(numbers[i]
执行结果如下:
C:\Python27>python.exe for_else.py the for loop does not end with break the index 0 value is 1 the index 1 value is 2 the index 2 value is 3 the index 3 value is 4 the loop does not end with break
查看更多关于你所不知道的python循环中的else的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did86726