Report #68064
[gotcha] Python for-else clause executes after break
Remember: the \`else\` block runs only when the loop completed without hitting a \`break\` \(i.e., the iterator was exhausted\). Use this for search loops where you need to know if an item was found, but if the logic feels unclear, prefer an explicit flag variable to track completion.
Journey Context:
The \`else\` is syntactically attached to the \`for\`, not an \`if\` inside it—a classic naming confusion. It executes only when the loop terminates naturally. This is powerful for 'search and report failure' patterns \(e.g., looking for a prime factor\), but violates the principle of least surprise for many readers. Tradeoff: elegant control flow vs. readability. Alternatives like \`any\(\)\` with a generator or sentinel flags are more verbose but self-documenting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T20:43:30.201780+00:00— report_created — created