简体   繁体   中英

Odd Nested Loop Doesn't Break Properly (Python 3.x)

The following code should print multiple lines of

1
2
3

mixed with lines of

0

However, what it actually prints is multiple lines of

1
1
1
1
3

mixed with lines of

0

Code:

boxes = []
for y in range(len(hmap)):
    for x in range(len(hmap[y])):
        w = 4
        h = 4

        minh = hmap[y][x]
        maxh = hmap[y][x]

        htemp = h
        while True:
            if y + htemp > len(hmap): break

            passes = False
            wtemp = w
            while True:
                if x + wtemp > len(hmap[y]): break

                for c in range(x, x+wtemp):
                    for r in range(y, y+htemp):
                        minh = min(minh,hmap[c][r])
                        maxh = max(maxh,hmap[c][r])

                        if maxh - minh > v:
                            print('1')
                            break
                    else:
                        print('2')
                        break
                else:
                    print('3')
                    break

                print('0')
                passes = True
                wtemp += 1

            if passes:
                boxes.append([x,y,wtemp-1,htemp])

            htemp += 1

            if not passes: break
  • hmap is a 2D array of float values that is passed to the function this code is in.

This segment of code is supposed to generate a series of rectangles for other (irrelevant) parts of code to use later on. Rectangles that "pass" (min/max values don't have a difference greater than v ) cause

0

to be printed. Rectangles that don't "pass" should cause

1
2
3

to be printed as the nested for and while loops break. Why doesn't it work?

While attempting to run your code I encountered an IndexError: list index out of range error. It appears that you may have transposed your column and row indices. Try changing the [c][r] subscripts to [r][c] :

# [...]
            for c in range(x, x+wtemp):
                for r in range(y, y+htemp):
                    minh = min(minh,hmap[r][c])
                    maxh = max(maxh,hmap[r][c])
# [...]

I am not sure if this is the cause of the incorrect breaks/prints, but it certainly could make a difference.

The code could be breaking the wrong loops, I could be wrong . For the while loop, make a Boolean variable and set it to true. Then inside a while loop, use an if statement to make it false when you want to.

top_loop, bottom_loop = True, True
while top_loop:
    # do something
    while bottom_loop:
        # do something
        if condition:
            top_loop = False

I haven't though about the for loops yet. There is answer here on this link with the naming for loops and breaking the for loops. It uses the contextlib library.

Link

It looks like the indentation on your code blocks is incorrect. There are else statements aligned with for statements, etc. Python uses indentation to separate blocks of code like this. Double-check that things are aligned properly, either in your code, or in what you copied here. If the indentation is just incorrect in the question here, feel free to edit it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM