【Python笔记】while...else与for...else语句

版权声明:看不尽的尘埃版权所有,转载请注明出处 https://blog.csdn.net/weixin_42936566/article/details/87860978

与别的编程语言不一样的是python还支持这样的语法

1、while...else...

2、for...else....

下面是代码:

#!/usr/bin/python
# -*- coding:utf-8 -*-
# wirter:En_dust

i = 0
while i < 5:
    i +=1
    print("当前变量i",i)
else:
    print("while结束后执行")


for c in range(0,10):
    print("当前变量c:",c)
else:
    print("for结束后执行")

下面是输出结果:

当前变量i 1
当前变量i 2
当前变量i 3
当前变量i 4
当前变量i 5
while结束后执行
当前变量c: 0
当前变量c: 1
当前变量c: 2
当前变量c: 3
当前变量c: 4
当前变量c: 5
当前变量c: 6
当前变量c: 7
当前变量c: 8
当前变量c: 9
for结束后执行

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_42936566/article/details/87860978