Help with loops! - beginner


I was answering a challenge question when I got stuck in a similar issue to this code, I solved the original challenge, yet I can’t think why this code doesn’t run till it prints “The end”.


x = [1,2,3,4] for n in x: if len(x) > 0: x.pop(0) print(x) else: break print("The end")

The presence of the break command means that print will never run. You’re deliberately breaking out of the loop right then and there.

There’s also a classic issue with this style of loop. If you modify the object you’re iterating through (in this case your list: x) then you’ll almost always run into trouble.

1 Like