请问第九行not skip意思是在什么条件下运行total+=e呢?为什么6和7之前的数也运行不了呢?


def sum67(nums):
    total=0
    skip=False
    for e in nums:
        if e==6:
            skip=True
        if e==7 and skip==True:
            skip=False
        elif not skip:
            total+=e
    return total
        
print(sum67([1,2,2,6,99,99,7]),",expect 5")

elif not skip: 就是skip为False时执行 total+=e
skip初始为False
第1次循环e=1 执行total+=e total为1
第2次循环e=2 执行total+=e total为3
第3次循环e=2 执行total+=e total为5
第4次循环e=6 skip改成True if e==7 and skip==True:判断不成立, elif not skip: 判断也不成立, 不执行total+=e
第5次循环e=99 skip还是True if e==7 and skip==True:判断不成立, elif not skip: 判断也不成立, 不执行total+=e
第6次循环e=99 skip还是True if e==7 and skip==True:判断不成立, elif not skip: 判断也不成立, 不执行total+=e
第7次循环e=7 if e==7 and skip==True:判断成立skip改成False, 因为if 判断成立对应的elif就不会再判断了, 还是不执行total+=e
最终 total为5

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632