B
22

Warning: My loop was running 47,000 extra times because I put a variable in the wrong place

I was debugging my first Python script last night and saw the output was way off, then realized I declared my counter inside the for loop instead of outside, resetting it every time, so now I triple check where I set my variables has anyone else fallen for this stupid mistake?
2 comments

Log in to join the discussion

Log In
2 Comments
ericj45
ericj4513d ago
Wait, isn't a counter inside the loop still counting just fine if you're adding to it each time and it resets to zero at the start of each iteration...? That sounds like a totally different problem, maybe you had it inside the loop but not resetting to the right value.
6
lucasschmidt
Yeah, you're probably right, @ericj45. I once had a counter inside a loop that was resetting to -1 instead of 0 every time, so it counted one less each go-round. Spent a whole afternoon staring at it before realizing my dumb mistake. Classic "off by one" thing, but with extra steps. Makes me wonder how many bugs I've written that just quietly work most of the time until they don't.
2