O
6

Back when I was learning to code, I kept messing up my loops for weeks

I was working on a project for a class and my loops kept running one time too many or not enough. I must have rewritten the same block of code twenty times. My friend in Boston told me to just add a print statement inside the loop to see what was happening each step. I thought it was too simple to help, but I tried it anyway. I wrote 'print(i)' inside my for loop and ran the script. Seeing the numbers pop up in the console showed me exactly where my logic was off. It was such a basic fix, but it saved me hours of staring at the screen. Does anyone else have a simple trick like that that just clicked for them?
3 comments

Log in to join the discussion

Log In
3 Comments
maxburns
maxburns22d ago
Oh, the classic print statement fix! I'm pretty sure that's how most of us learned to stop hating loops.
0
the_blair
the_blair22d ago
Wait, @maxburns, you learned to stop hating loops? The print statement just showed me how broken my logic was. I'd stare at a wall of numbers going the wrong way, realizing my counter started at ten instead of zero. It didn't make me love loops, it just made the pain visible. That's how I learned to fear off-by-one errors forever.
4
uma_baker88
Fear forever" seems a bit much. It's just a loop, you get used to it.
3