python中else的用法_python中else的用法

for+else

1 defelse1(num):2 for i inrange(num):3 if(i>10):4 break

5 print(i)6 else:7 print("over") #当for循环正常结束时,会执行该条语句8

9

10 else1(5)11 print("##"*20)12 else1(20)13

14 结果:15

16 017 1

18 2

19 3

20 4

21 over22 ########################################

23 024 1

25 2

26 3

27 4

28 5

29 6

30 7

31 8

32 9

33 10

while+else

1 defelse2(num):2 while num >0:3 if(num==10):4 break

5 num-=1

6 print(num)7 else:8 print("over") #当while循环正常结束时,会执行该条语句9

10

11 else2(5)12 print("##"*20)13 else2(20)14

15 结果:16 4

17 3

18 2

19 1

20 021 over22 ########################################

23 19

24 18

25 17

26 16

27 15

28 14

29 13

30 12

31 11

32 10

try+else

1 defex(num):2 if(num==10):3 raise BaseException("BaseException!")4

5 defelse3(num):6 try:7 for i inrange(num):8 ex(i)9 print(i)10 exceptBaseException as msg:11 print(msg)12 else:13 print("over")14 finally:15 print("end")16

17 else3(5)18 print("##"*20)19 else3(20)20

21 结果:22 023 1

24 2

25 3

26 4

27 over28 end29 ########################################

30 031 1

32 2

33 3

34 4

35 5

36 6

37 7

38 8

39 9

40 BaseException!41 end

原文:https://www.cnblogs.com/moonpool/p/11921368.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值