python中else的研究

print('--------single else:if break--------')
for l in [1, 9, 0]:
    if l % 3 == 0:
        print("if")
        break
else:
    print("else")
    print(l)

print('--------single else:no if--------')
for m in [1, 9, 0]:
    print("for")
    break
else:
    print("else")
    print(m)

print('------single else:no break--------')
for n in [1, 9, 0]:
    if n % 3 == 0:
        print("if")
else:
    print("else")
    print(n)

print('------single else:no if break--------')
for o in [1, 9, 0]:
    print("for")
else:
    print("else")
    print(o)

print('------double else:no break--------')
for i in [1, 9, 0]:
    if i % 3 == 0:
        print("if")
    else:
        print("inner else")
else:
    print("outer else")
    print(i)

print('--------double else:if break--------')
for j in [1, 9, 0]:
    if j % 3 == 0:
        print("if")
        break
    else:
        print("inner else")
else:
    print("outer else")
    print(j)

print('--------double else:else break--------')
for k in [1, 9, 0]:
    if k % 3 == 0:
        print("if")
    else:
        print("inner else")
        break
else:
    print("outer else")
    print(k)

print('------double else:break if --------')
for p in [1, 9, 0]:
    if p % 3 == 0:
        if p == 2:
            print("if")
            break
        else:
            print("inner else")
else:
    print("outer else")
    print(p)

print('------double else:break anyway --------')
for q in [1, 9, 0]:
    if q % 3 == 0:
        if q == 2:
            print("if")
        else:
            print("inner else")
            break
else:
    print("outer else")
    print(q)

打印结果如下

--------single else:if break--------
if
--------single else:no if--------
for
------single else:no break--------
if
if
else
0
------single else:no if break--------
for
for
for
else
0
------double else:no break--------
inner else
if
if
outer else
0
--------double else:if break--------
inner else
if
--------double else:else break--------
inner else
------double else:break if --------
inner else
inner else
outer else
0
------double else:break anyway --------
inner else

Process finished with exit code 0

答案是如果触发了breakelse不执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值