0

The while loop in my code refuses to terminate when I use the break statement, and I'm really confused why this is happening.

point = 0
while point < len(list_of_line_lists) - 1:
    sentence = list_of_line_lists[point]["text"]
    datapoint = point
    while True:
        try:
            if list_of_line_lists[datapoint]["author"] == list_of_line_lists[datapoint + 1]["author"]:
                sentence += list_of_line_lists[datapoint + 1]["text"]
                datapoint += 1
                print(datapoint)
            else:
                print("this isn't breaking")
                break
        except IndexError:
            break

In my code above, the break statement within the else statement refuses to trigger. The code within it is executed, as there's a flurry of "this isn't breaking" in the output, but the loop itself doesn't terminate. This is how my output looks(until I manually stop it myself):

this isn't breaking
this isn't breaking
this isn't breaking
this isn't breaking
this isn't breaking
this isn't breaking
this isn't breaking

Any ideas to why this is happening?

WiseRohin
  • 15
  • 4
  • 3
    You have two `while` loops, and the `break` will only exit the inner one. Have you put any any code at the end (or beginning) of the outer `while` loop to see if in fact the problem is there? Your outer loop is looking at the value of the `point` variable, and it doesn't look like you're ever changing that (or the `list_of_line_lists` variable inside your loop. – larsks Jan 09 '21 at 23:43
  • 1
    It breaks the inner loop but not the outer loop. – Klaus D. Jan 09 '21 at 23:44
  • Okay, yeah that was a moronic mistake, I forgot to increment the point variable. I really am lacking sleep, and thanks a lot for the advice! @larsks – WiseRohin Jan 10 '21 at 00:01
  • see [How to break out of multiple loops?](https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops) – AcK Jan 10 '21 at 00:07

1 Answers1

0

The inner loop is breaking, but the outer loop isn't. The reason is because you are using the point variable in the outer while loop, but that (point) variable value is never changed (incremented/decremented).