15
The day I realized my if statements were backwards for 6 months
I was working on a little number guessing game in Python, my first real project. It worked fine when the user guessed too high, but totally broke when they guessed too low. I spent like 3 evenings staring at my code, adding print statements everywhere. Then my buddy on Discord asked me to read my condition out loud, line by line. I got to the line that said "if guess < number" and I said it as "if guess is less than the secret number", which is correct. But my code had it checking the wrong branch. The moment I said it out loud, I just went quiet. I had swapped the greater than and less than signs in the if and elif blocks, so the low guess logic was running for high guesses. It was such a dumb 2 character mistake, but it taught me to always trace through my logic with a paper example before touching the code. Has anyone else caught a bug just by explaining their code to a rubber duck or a friend? I feel like that trick should be taught on day one.
1 comments
Log in to join the discussion
Log In1 Comment
betty_shah6d ago
You ever spend an entire weekend convinced your code was possessed? lol I feel this so hard. I once had a loop that was incrementing the wrong variable, but only failed on Tuesdays, or so I thought. Turns out the data file I was testing with got updated at midnight, so it really was just a time thing mixed with a dumb typo. Now I literally keep a little dinosaur toy on my desk and I read my code out loud to it before I even open the debugger. Paper tracing sounds boring, but honestly, forcing yourself to write out what each line does with fake numbers catches way more than just staring. That 2 character fix probably saved you from a bigger headache later, trust me.
2