众多语言中都有if else这对条件选择组合,但是在python中还有更多else使用的地方,比如说循环for,或者while都可以和else组合。
下面简单介绍一下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)) breakelse: print('the for loop does not end with break') i = 0while(numbers[i] 5): print('the value is %d '%(n)) breakelse: print('the for loop does not end with break') i = 0while(numbers[i]执行结果如下:
C:Python27>python.exe for_else.pythe for loop does not end with breakthe index 0 value is 1the index 1 value is 2the index 2 value is 3the index 3 value is 4the loop does not end with break登录后复制
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2283659.html