0

Cant figure out why the break is not working below: Loaded the already existed excel file using openpyxl.

I tried printing the values before break, and it does print the row values that are empty. This confirms that the inside of loop is executed.

def x(ws):
    for row in ws.iter_rows(min_row=11,max_row=50,min_col=4,max_col=4):
            for cell in row:
                if cell.value is None:
                    next_index=int(cell.row)
                    break

    print('The return is: ')
    print(next_index)


    return next_index

All I want is exit the loop with the first cell empty on that range but instead getting the last row i.e 50 return, where as expected return is 11.

ashim
  • 67
  • 1
  • 2
  • 12

1 Answers1

1

Your break statement will only break out of the inner for loop. Here's another relevant stackoverflow question: How to break out of multiple loops in Python?

As suggested there you can just return next_index from the inner for loop instead of trying to break out of both loops to return the appropriate value.

azundo
  • 5,902
  • 1
  • 14
  • 21
  • 1
    oh! yeah it keeps looping for entire range because of multiple loops....thanks the return is the easiest way to fix and what I need :) cheers – ashim Jul 29 '19 at 03:49