如何回到无限循环内的某一点

时间:2016-05-02 03:33:51

标签: python-3.x

我有安装goto的问题,但我也不确定我是否想在我的代码中使用它,所以如果在内部而真正的无限循环我想回到某个点,条件下面的某个地方循环,如何在python中弄清楚它?

这里是一个内部结构,而真正的无限循环,如果我可以得到它没有goto

x = 0

#some code

a + b # just some place in code, which must be return point if further condition is true

if x > 0: # if x is not 0 make it 0
     x = (x == 0)

#some code

if y == 1: # some result to make further condition true
     x = (x + 1)

if x == 1:        
    # and here if I have this condition from above go to a + b and start from there

#some code

1 个答案:

答案 0 :(得分:2)

您可以使用while Truecontinuebreak获得该行为。无需模块。

x = 0

#some code

while True:

    a + b # just some place in code, which must be return point if further condition is true

    if x > 0: # if x is not 0 make it 0
        x = (x == 0)

    #some code

    if y == 1: # some result to make further condition true
        x = (x + 1)

    if x == 1:        
        # and here if I have this condition from above go to a + b and start from there
        continue
    break

#some code

编辑:功能

def other_function():
    a = 1
    while True:
        # some code
        if a == b:
            continue
        # some more code
        if sin(a) < sqrt(a**2 + b**2):
            break:
        # more code
    # still more code

def main():
    # some code
    while True:
        # some more code
        other_function()
        # still more code
    # after a while
    exit(0)