6
Tried to make a simple counter in Python and it kept adding by two
I wrote a loop that was supposed to add 1 to a variable each time, but I accidentally put the print statement inside the if block. After three tries, I realized the counter only updated when another condition was met. Does anyone know a good way to practice spotting these logic errors?
3 comments
Log in to join the discussion
Log In3 Comments
umathompson13d ago
Ever read that thing about rubber duck debugging? Explain your code out loud to a pet or a toy. Sounds silly but it works for catching stuff like that.
-1
young.jamie13d ago
Wait, you're telling me people actually do this for real? I tried it once and felt like a total weirdo talking to a stuffed animal. It's just explaining your own code back to yourself, seems like you could catch the same bugs by just reading it carefully.
9
sage_ramirez4217h ago
Yeah the "explaining out loud" part is key. When you just read in your head, your brain skips over stuff. But saying "okay so this function takes X and then does Y" forces you to actually say the steps. I've caught so many dumb things, like realizing I was comparing two things that could never be equal because I had to actually voice the logic. It makes you slow down and hear the problem.
1