Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does my function show the right answer but give me None when I save its result? For example, I see 12 in the output, but answer becomes None.

Geez

The likely issue is that the function prints its answer instead of returning it. Printing displays text for you to see; it does not hand that value back to the code that called the function. If a function reaches its end without a return, Python returns None to the caller. [S1]

For example, suppose your function is:

def add(a, b):
    print(a + b)

answer = add(5, 7)
print("Saved value:", answer)

The first print displays 12. The final print displays Saved value: None, because add did not return the sum. The function’s visible output and its returned value are separate things. To diagnose your own code, find the line that computes the intended result, then check whether that value is returned from the function.

Follow-up question

What should the corrected version look like? I want to store the result and use it later, not just display it.

Geez

Replace print inside the function with return when the goal is to give the computed value back to the caller:

def add(a, b):
    return a + b

answer = add(5, 7)
print("Saved value:", answer)

Now add(5, 7) produces a returned value that gets assigned to answer; the later print displays Saved value: 12. In general, return expression sends the expression’s value back. By contrast, a bare return, or reaching the end of the function without a return, gives the caller None. [S1]

You can still print the result if you want to show it. Just do so at the point where display is useful, rather than using a print statement inside the function as a substitute for returning a value.

Follow-up question

I added a return, but I still get None on some inputs. What should I check?

Geez

Check every path through the function, not just the path you tried first. A return inside an if branch only handles calls that enter that branch. If another path reaches the end of the function without returning a value, that call returns None.

For instance, this version returns a result only when amount is positive:

def double_if_positive(amount):
    if amount > 0:
        return amount * 2

If the function is called with a value that does not enter the condition, execution reaches the end without a return. Decide what result is intended for that case, then add an appropriate return on that path. If you do not intend to support that case, make the behavior explicit for the reader of your code. Also check for a bare return: it does not return an expression’s value; it returns None. [S1]

Follow-up question

Is there a quick way to tell whether I should use print or return? I often want to see the answer while also passing it to another part of my program.

Geez

Ask what the function’s caller needs. Use return when later code should receive and work with the result. Use print when you want to display a value. If you need both, return the value and print it separately:

def total(price, fee):
    return price + fee

bill = total(8, 4)
print("Total:", bill)

Here, the function supplies a value for bill; the caller chooses to display it. A useful check is to inspect the assignment and the function together: if you write result = your_function(...), make sure the function returns the intended expression along every path you expect to use. The Python tutorial establishes the return behavior described here; it does not determine what result your particular program should produce for every input. That choice depends on your function’s intended behavior. [S1]

A practical checklist

  • Find the expression that computes the value you expected.

  • Check whether the function uses print where it should use return.

  • Trace each expected path through the function and confirm it returns a value.

  • Check that a bare return is 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.

  1. [S1] Python Tutorial: More Control Flow Tools — Function return values
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work