Why your function result is None — and how to fix it.
Timothy beamed as he looked at his screen. He had just finished refactoring his pricing script into neat, organized functions, exactly as Margaret had shown him.
"It feels so much cleaner now," he said, hitting Run. "Let's see it working."
The screen flashed.
Calculating Tax...
Tax is: 15.0
Final Price: None
Timothy’s smile faded into a look of genuine puzzlement. "That's odd," he said softly. "The final price is None. But right there on the line before, it printed '15.0'. I wonder where the number went?"
Margaret looked up from her desk and smiled. "Do you want a second pair of eyes?"
"Please," Timothy said. "I'm not sure what I missed. Inside calculate_tax, I calculated the tax variable. It definitely holds 15.0 because I printed it. But when I try to use it in the main function, it seems to vanish."
He showed her the code:
def calculate_tax(price, rate):
tax = price * rate
print(f"Tax is: {tax}")
def main():
price = 100
rate = 0.15
# Timothy expects 'tax_amount' to hold 15.0
tax_amount = calculate_tax(price, rate)
final_price = price + tax_amount
print(f"Final Price: {final_price}")
if __name__ == "__main__":
main()
The Implicit None
Margaret nodded kindly. "I see exactly what's happening. You have done the calculation perfectly, but you haven't told the function to hand the answer back to you."
She walked to the whiteboard. "Remember how we talked about functions being like separate rooms? Right now, you are doing the math inside the room and shouting the answer out the window (print). But the main function is waiting outside the door for you to hand it a note."
"Python has a quiet rule," Margaret continued. "Every function must return something. If we don't explicitly tell it what to return, Python politely returns None by default."
Timothy looked at his code with fresh eyes. "So when I wrote tax_amount = calculate_tax(...), I wasn't capturing the 15.0. I was capturing that default None?"
"Exactly," Margaret said. "You calculated the number, showed it to us, but then the function ended without passing the note under the door."
Passing the Baton
"That makes sense," Timothy said. "So I need to officially send the data back."
"Precisely," Margaret said. "We use the return keyword. Think of it like a relay race. The function runs its lap, and return is the moment it hands the baton to the next runner."
She added a gentle warning. "Just remember: return is the finish line. Once a function returns a value, it stops running immediately. It’s the final handoff."
She helped him modify the code:
def calculate_tax(price, rate):
tax = price * rate
return tax # Explicitly hand the value back
def main():
price = 100
rate = 0.15
# Now, the function hands back 15.0
tax_amount = calculate_tax(price, rate)
# And we can use it for math!
final_price = price + tax_amount
print(f"Final Price: {final_price}")
if __name__ == "__main__":
main()
Output:
Final Price: 115.0
"There it is," Timothy smiled. "The print is for me, but the return is for the code."
"A perfect way to put it," Margaret agreed. "You're getting really good at thinking about how data flows through the system, Timothy."
Margaret’s Cheat Sheet
Margaret opened her notebook to the "Functions" section.
- The Concept: Data Flow vs. Side Effects.
- The Trap: Assuming that calculating or printing a value makes it available to the rest of the program.
-
The Rule: By default, if a function reaches the end without a
returnstatement, it returnsNone. -
The Fix: Use
returnto pass the result back to the caller. -
print(x)= Show it to the human (Side Effect). -
return x= Give it to the computer (Data Flow).
In the next episode, Margaret and Timothy will face "The Shadow Name"—where Timothy learns why naming a variable list or str can cause surprising issues.
Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.
Top comments (4)
This is such a clear explanation of a super common beginner mistake. The line “print is for humans, return is for the code” really sticks — I wish I heard that when I started learning Python. Simple, relatable, and very helpful.
🙏✨
Then the difference all lies in the third line "return tax"
it is intersting to be shocked when the computer fails to do what we (humans )do all the time It sometimes behaves as a todler learning his first steps or a dumb students always needs the teacher explains what we all regard as a self evident!
🙏💯✨