It is returning None because when you recursively call it:

if my_var != "a" and my_var != "b":
    print('You didn\'t type "a" or "b". Try again.')
    get_input()

...you don't return the value.

So while the recursion does happen, the return value gets discarded, and then you fall off the end of the function. Falling off the end of the function means that python implicitly returns None, just like this:

>>> def f(x):
...     pass
>>> print(f(20))
None

So, instead of just calling get_input() in your if statement, you need to return what the recursive call returns:

if my_var != "a" and my_var != "b":
    print('You didn\'t type "a" or "b". Try again.')
    return get_input()
Answer from roippi on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › eli5 how does "none" work exactly?
r/learnpython on Reddit: ELI5 how does "None" work exactly?
October 6, 2023 -

I get the general idea that None is a type when there is no specific value to be return.

But I am just confused on how to explicitly have a value of "None" . I am still learning python so I want to understand in which case does a function returns the value of "None". Like, it would also be helpful is someone could share a short function that displays "None" , preferably with no "return".

I've search online and there was an example of print(print("hi")), I kind of get this, the interpreter displays the first print and the second one is "None" because there is no value.

Thanks in advance

🌐
Python.org
discuss.python.org › python help
Why does one version of my function return None, while the other works fine? - Python Help - Discussions on Python.org
October 26, 2023 - Inspired by the pipeline operator (|>) in other languages, I tried to implement a function to imitate it. When I use reduce to implement it, it results in None somewhere along the way, but another one using loops works just fine. from functools import partial, reduce from pprint import pprint def pipeline(data, *funcs): res = data for func in funcs: res = func(res) return res def pipeline_v2(data, *funcs): reduce(lambda arg, func: func(arg), funcs, data) def main() ->...
🌐
Quora
quora.com › What-does-it-mean-when-the-Python-function-returns-none
What does it mean when the Python function returns none? - Quora
Answer (1 of 6): There are no inherently implied semantics attached to None-returning functions. In other words, it means whatever you want it to mean. In Python, interpreted and dynamically typed as it is, functions don't have return types. There is no type checker to enforce that a function re...
🌐
Python.org
discuss.python.org › python help
Why does the my code return none!? I’m a noob so I don’t know whether the question is overtly stupid or what😃 - Python Help - Discussions on Python.org
July 25, 2021 - This returns none. Ideal behind program is to convert Celsius to Fahrenheit using Fahrenheit = (9/5) * celsius + 32 But when I write return (9/5) * c + 32 it gives the desired result Just curious on the difference between print ((9/5) * c + 32) and return
Top answer
1 of 5
787

On the actual behavior, there is no difference. They all return None and that's it. However, there is a time and place for all of these. The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.

Using return None

This tells that the function is indeed meant to return a value for later use, and in this case it returns None. This value None can then be used elsewhere. return None is never used if there are no other possible return values from the function.

In the following example, we return person's mother if the person given is a human. If it's not a human, we return None since the person doesn't have a mother (let's suppose it's not an animal or something).

def get_mother(person):
    if is_human(person):
        return person.mother
    else:
        return None

Using return

This is used for the same reason as break in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.

We've got 15 prisoners and we know one of them has a knife. We loop through each prisoner one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the prisoners. If we don't find the prisoner with a knife, we raise an alert. This could be done in many different ways and using return is probably not even the best way, but it's just an example to show how to use return for exiting a function.

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()

Note: You should never do var = find_prisoner_with_knife(), since the return value is not meant to be caught.

Using no return at all

This will also return None, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.

In the following example, we set person's mother's name and then the function exits after completing successfully.

def set_mother(person, mother):
    if is_human(person):
        person.mother = mother

Note: You should never do var = set_mother(my_person, my_mother), since the return value is not meant to be caught.

2 of 5
57

Yes, they are all the same.

We can review the interpreted machine code to confirm that that they're all doing the exact same thing.

import dis

def f1():
  print "Hello World"
  return None

def f2():
  print "Hello World"
  return

def f3():
  print "Hello World"

dis.dis(f1)
    4   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    5   5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f2)
    9   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    10  5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f3)
    14  0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE            
        5 LOAD_CONST    0 (None)
        8 RETURN_VALUE      
🌐
Real Python
realpython.com › python-return-statement
The Python return Statement: Usage and Best Practices – Real Python
June 14, 2024 - Say you’re writing a function that adds 1 to a number x, but you forget to supply a return statement. In this case, you’ll get an implicit return statement that uses None as a return value: ... >>> def add_one(x): ... # No return statement at all ... result = x + 1 ... >>> value = add_one(5) >>> value >>> print(value) None · If you don’t supply an explicit return statement with an explicit return value, then Python will supply an implicit return statement using None as a return value.
🌐
Finxter
blog.finxter.com › python-function-return-none-without-return-statement
Python Function Return None Without Return Statement – Be on the Right Side of Change
The Python return keyword allows you to define the return value of a function (e.g., return X). If you don’t explicitly set a return value or you omit the return statement, Python will implicitly return the following default value: None.
Find elsewhere
🌐
Better Programming
betterprogramming.pub › please-stop-returning-none-from-python-functions-ff98e492b4b0
Stop Returning None From Python Functions | by Imran Ali | Better ...
August 10, 2023 - The function returns None when the denominator is 0. This makes sense as the result is undefined so sending None sounds natural.
🌐
AskPython
askpython.com › home › python return statement
Python return Statement - AskPython
May 11, 2023 - So, either you have used a return statement with no value, or there is no return statement, and the function returns None. A function can have multiple returns, the one that satisfies the condition will be executed first and the function will exit.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-none-printed
Why does my function print None in Python [Solved] | bobbyhadz
Functions often print `None` when we pass the result of calling a function that doesn't return anything to the `print()` function.
🌐
Julianhysi
julianhysi.com › post › 4
Julian's Blog - Bare return vs return None vs no return
Every function in Python which does not hit a return statement, will implicitly return None.
🌐
Codecademy
codecademy.com › forum_questions › 54f64ad795e3788c240004cc
17/19 code looks OK but return returns "None". | Codecademy
s = "yes" def shut_down(s): if s == "yes": return "Shutting down" elif s == "no": return "Shutting down aborted" else: return "Sorry" print shut_down(s)
🌐
Team Treehouse
teamtreehouse.com › community › python-function-returns-a-none-value-for-no-reason
python function returns a none value for no reason (Example) | Treehouse Community
January 30, 2016 - I haven't explicitly tested, but it's likely because this is intended to be a recursive function, but you don't return the call when it's called from within itself. Instead, when called from inside itself, the innermost return value is discarded, and None is passed up and returned by the original invocation.
🌐
Real Python
realpython.com › lessons › returning-none-explicitly
Returning None Explicitly (Video) – Real Python
As you’ve seen, Python will return a value of None in several situations, but there might be occasions to actually write out a return None statement. 00:19 There are basically three ways to cause a value of None to be returned from a function: if the function doesn’t have a return statement at all, if you have a return statement with no return value, or you can explicitly return None.
Published   August 10, 2021
🌐
YouTube
youtube.com › watch
How to stop Python function from returning None - YouTube
This tutorial explains about various situations where a Python function returns None as output. And also examples show, how to stop python function from retu...
Published   June 12, 2023
🌐
Finxter
blog.finxter.com › home › learn python blog › python return nothing/null/none/nan from function
Python Return Nothing/Null/None/NaN From Function - Be on the Right Side of Change
August 1, 2023 - In Python, a function returns None implicitly if there is no explicit return statement. However, you can also use the return statement followed by the keyword None to indicate that a function should return no value.