2
I just learned that Python's 'else' works on loops too
I was working through a tutorial on freeCodeCamp and stumbled across something that blew my mind. You can use an 'else' block on a for or while loop in Python, not just if statements. The else runs after the loop finishes normally, but if you hit 'break' it gets skipped. I must have written hundreds of loops without knowing this existed. It's super handy for search patterns where you want to do something if no match was found. Has anyone else missed a basic feature like this and felt silly when they discovered it?
2 comments
Log in to join the discussion
Log In2 Comments
evanpalmer1mo ago
My buddy Dave spent 4 hours debugging before someone showed him that.
5
andrew71mo ago
Gotta LOVE those "obvious" fixes that hide for hours.
0