4
Shoutout to finally getting loops right in Python after weeks of struggling
For the longest time I would write out every single step by hand, copying and pasting code blocks over and over just to handle a list of 20 items. Two days ago I finally sat down and forced myself to understand a simple for loop with a range function, and it cut my last project from 150 lines to about 30. Has anyone else had that moment where one basic concept just clicks and changes how you write everything?
3 comments
Log in to join the discussion
Log In3 Comments
kimfisher1mo ago
Honestly I gotta push back on that a little, especially the part about writing things out step by step letting you see what's happening. When you copy and paste the same block 20 times and then need to change something, you have to edit all 20 copies manually. That's how bugs slip in, you miss one line in one copy and suddenly your output is wrong for just that one item. At least with a loop you change it once and it applies everywhere. But yeah the off by one thing is real, I still add print statements inside my loops to check the index value when I first run them.
4
the_tessa1mo ago
Wait, is nobody gonna say that loops can actually make things harder sometimes? I mean yeah they save lines but if you mess up the logic you got a bug that runs 200 times before you even catch it. I spent like 3 hours last week debugging a loop that was off by one and it drove me up a wall. The old way of writing things out step by step is boring but at least you can see exactly what it's doing every time. Loops are great for simple stuff but when you gotta handle weird edge cases or conditional logic inside them it can get messy real fast. I guess my point is don't act like loops are some magic fix for everything because they got their own headaches too.
2
@the_tessa But don't you think the payoff of learning loops outweighs the debugging headache once you actually get it? I see what you're saying about off-by-one errors, those suck, but the time you save on writing out every single line by hand is huge. The trick for me was just accepting that I'd mess up the first few times before it clicked.
1