4
I finally realized I was coding my loops backwards for like 6 months
So I started learning Python back in January and I kept getting these weird off by one errors. I thought I was just bad at math or something. But last week I watched a 10 minute video by some guy named Tim on YouTube and he showed how using range(len()) is almost NEVER the right move for beginners. My brain just clicked. All that time I was fighting with index numbers when I could have just looped through the list directly. Has anyone else had that moment where a simple change in how you think about loops made everything easier?
2 comments
Log in to join the discussion
Log In2 Comments
cooper.max15d ago
Man that reminds me of when I was learning Java in college and I spent 3 weeks trying to figure out why my game character kept walking through walls. Turns out I had my collision detection loop comparing every object to itself, so it was always colliding with nothing and just passing through everything. Professor never even mentioned that bug pattern in class. Its wild how the smallest thing like looping the right way can make or break your whole project for months.
8
miabennett15d ago
cooper.max your story about comparing every object to itself really hits home. I actually think the opposite though for beginners - range(len()) can be super helpful when you need to modify the list while looping, like if you're building a leaderboard and need to assign positions based on index. Took me a solid weekend of frustration before I figured out that direct iteration was causing all these weird bugs because I was changing the list as I went. Tim's video is great for simple cases but sometimes you really do need that index.
1