Conversation

Replying to
for x in range(1,10): while True: print(x) break This gives me the output (1,2,3,4,5,6,7,8,9) which I would expect. It is breaking out of the inner while loop but staying in the outer for loop. What output would you expect?
1
Replying to
Not saying the behavior was undefined. Without syntax python lacks, it can only break out of the inner loop then. As a casual reader or a maintenance dev, I'd always assume that the original developer didn't know what would happen-an inner or outer loop break (1) vs (1,2...9)
2
1
I've never found its behavior confusing, but I suppose others could. Outer loop breaks can be achieved by setting a boolean flag before breaking or with for/else, but usually better to extract a function in this case.
1
2