O
19

When my function kept returning 'None' and I couldn't figure out why

I was making a simple program to add numbers in Python. My function looked right, but every time I called it, I got 'None'. I spent way too long checking the math and printing variables. Turns out, I just forgot to write 'return' at the end. It was a silly mistake that taught me to read my code more carefully. Has anyone else had a similar experience with beginner errors?
3 comments

Log in to join the discussion

Log In
3 Comments
wendy730
wendy7302mo ago
Ugh, doesn't that just make you want to scream? It's like your code is gaslighting you. That mistake is so common it's practically a rite of passage. What finally clicked for me was forcing myself to trace the program flow, step by step, in my head or on paper. You realize the function does its work, then just drops the answer on the floor because it has no way to send it back. It really drills in that writing the steps isn't enough, you have to explicitly say what comes out of it.
6
johnw71
johnw712mo ago
Yeah I spent three days on that exact problem when I first started... my function would calculate everything right but it was like shouting into a void. Finally my friend pointed at the screen and just said "you forgot to give it back." Felt so obvious once someone said it.
1
emma303
emma3031mo ago
Oh man, @johnw71, that "shouting into a void" feeling is exactly right. I started writing my return statements first, before the function logic, just to force the habit. It saves so much pointless staring at the screen.
1