如何打破嵌套的while循环

时间:2014-04-07 22:13:38

标签: python nested-loops break

我无法摆脱while循环。这是我的代码

while (True):
    if (dem_arr[randx, randy] > -100):
        ticker = 0
        while stack:
            x, y = stack.pop()
            mask[x, y] = True
            for dx, dy in neighbors:
                nx, ny = x + dx, y + dy
                print ticker
                if (0 <= nx < dem_arr.shape[0] and 0 <= ny < dem_arr.shape[1] and dem_arr[x, y] > -100 and dem_arr[nx, ny] > -100 and not mask[nx, ny] and abs(dem_arr[nx, ny] - dem_arr[x, y]) <= 1):    #set elevation differnce
                    stack.append((nx, ny))  #if point is selected (true) array position gets added to stack and process runs over again
                    if ((nx, ny) not in counterStack):
                        counterStack.append((nx, ny)) 
                        dem_copy[(nx, ny)] = 8888
                        if (ticker >= 121):
                            print 'ticker ticked'
                            #if in this for loop need to break out of while stack:
                        else:
                            ticker += 1
    else:   
        print '!!!Point chosen has no data!!!'
        randx = random.randint(0, row-1)    #array begins at position 0,0
        randy = random.randint(0, col-1)
        continue

我需要输入的是if (ticker >= 121):我需要突破while stack:while(True).有关如何执行此操作的任何想法吗?

3 个答案:

答案 0 :(得分:0)

一种可能的解决方案是使用变量来跟踪(在这种情况下为breakout):

while (True):
    if (dem_arr[randx, randy] > -100):
        ticker = 0
        breakout = False
        while stack and not breakout:
            x, y = stack.pop()
            mask[x, y] = True
            for dx, dy in neighbors:
                nx, ny = x + dx, y + dy
                print ticker
                if (0 <= nx < dem_arr.shape[0] and 0 <= ny < dem_arr.shape[1] and dem_arr[x, y] > -100 and dem_arr[nx, ny] > -100 and not mask[nx, ny] and abs(dem_arr[nx, ny] - dem_arr[x, y]) <= 1):    #set elevation differnce
                    stack.append((nx, ny))  #if point is selected (true) array position gets added to stack and process runs over again
                    if ((nx, ny) not in counterStack):
                        counterStack.append((nx, ny)) 
                        dem_copy[(nx, ny)] = 8888
                        if (ticker >= 121):
                            print 'ticker ticked'
                            #if in this for loop need to break out of while stack:
                            breakout = True
                            break
                        else:
                            ticker += 1
    else:   
        print '!!!Point chosen has no data!!!'
        randx = random.randint(0, row-1)    #array begins at position 0,0
        randy = random.randint(0, col-1)
        continue

答案 1 :(得分:0)

考虑已经发表的评论。另外,想想while循环。而True:本质上是一个无限循环。你可以从一个函数中返回调用者,你可以在与循环相同的级别中断,或者你可以用一个在适当条件下开始False并变为True的表达式替换True。

编辑: 你不再用Java或C编程了。无需在括号周围加上括号&#34; True&#34;。 :) - 或其他条件。

答案 2 :(得分:0)

一个简化的例子,说明了使用函数控制内循环的概念:

stack = range(1, 500)

def stack_loop():

    ticker = 0

    while stack:
        x = stack.pop()
        # Your implementation here
        if ticker >= 121:
            print("Ticker ticked")
            return True
        else:
            print("Ticker increased")
            ticker += 1

    return False

while True:
    if stack_loop():
        break

将内循环的逻辑移动到外部函数,并使用return语句来控制是否必须中断主循环。

希望有所帮助:)

编辑:您还可以将整个块移动到该功能,只需return即可:

stack = range(1, 500)

def main_loop():

    while True:
        ticker = 0
        while stack:
            x = stack.pop()
            # Your implementation here
            if ticker >= 121:
                print("Ticker ticked")
                return
            else:
                print("Ticker increased")
                ticker += 1

main_loop()