Why does my Python function return None when I expected a value?
Quick answer
A Python function returns None if it reaches the end without a return statement. Printing a value only displays it; it does not pass that value back to the caller. Use return expression to give the caller a result, then print or otherwise use the function call’s returned value where needed. A bare return also returns None.
The question, the details, the next step.
A practical checklist
Find the expression that computes the value you expected.
Check whether the function uses
printwhere it should usereturn.Trace each expected path through the function and confirm it returns a value.
Check that a bare
returnis not being mistaken for returning a result.Print the returned value at the call site if you want to display it.
A few more useful answers
Does `print()` return the value it displays?
No. Printing displays output; it does not return the displayed value from your function. Return the computed value separately if the caller needs it.
What does a bare `return` do?
A bare return returns None, just like reaching the end of a function without a return statement. [S1]
Can I return a value and print it too?
Yes. Return the value from the function, then print the result of calling the function wherever you want it displayed.
Sources
References used for this guide.
- [S1] Python Tutorial: More Control Flow Tools — Function return values docs.python.org · Checked
