O
2

Struggled with a bug for hours until I tried this simple print trick

I was trying to write a function to sort a list of numbers in Python last night, and it kept giving me weird results. I spent like two hours staring at the code, sure my logic was right. Finally, I just added a print statement inside the loop to show the list each time it ran. It turned out I was accidentally changing the original list while trying to make a sorted copy, so the numbers got all mixed up. Seeing the list change step by step in the console made the mistake super obvious. I fixed it by using .copy() and it worked first try. That one print line saved me a whole evening of headache. Has anyone else found a simple debugging trick that saved them when they were starting out?
3 comments

Log in to join the discussion

Log In
3 Comments
garcia.laura
Ever try rubber duck debugging with an actual rubber duck?
3
patricialee
patricialee18d agoMost Upvoted
Honestly I used to think the whole rubber duck thing was just a silly meme for programmers lol. But I finally tried it the other day when I was stuck on some code and it actually helped me talk through the problem out loud. The duck just sits there staring at me and it makes me slow down and explain things step by step instead of just rushing through it in my head. I definitely get why people swear by it now.
9
margaret_taylor42
Nah, I just talk to myself like a crazy person lol. Works better than a duck for me, @garcia.laura.
4