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?
2 comments

Log in to join the discussion

Log In
2 Comments
wendy730
wendy7304d 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
johnw714d 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