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

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐