python3 goto跳转到指定代码行,执行代码

1.需求背景:

当执行到某一步骤后,发现结果不是想要的那种形式,希望这一次循环重新执行,需要跳转到固定位置。

2.使用goto:

(1)安装goto

pip install goto-statement

(2)使用goto完成一个小例子

官方文档见:https://pypi.org/project/goto-statement/

注意:如果你在ide山运行label 和 goto 下有红色波浪线提示错误。不用理会直接执行即可

from goto import with_goto

@with_goto     #必须有
def te(list_):
    tmp_list = list_
    label.begin        #标识跳转并开始执行的地方
    result = []
    try:
        for i, j in enumerate(list_):
            tmp = 1 / j
            result.append(tmp)
            last_right_i = i
            if i == 1:
                print('----hhhhhhh')
                goto.begin
    except ZeroDivisionError:
        del tmp_list[last_right_i + 1]
        goto.begin      #在有跳转标识的地方开始执行
    return result

if __name__ == '__main__':
    a = te([1, 3, 4, 0, 6])
    print(a)

参考:
https://blog.csdn.net/weixin_43389082/article/details/107058787

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值