Why Does the Print Function Return None

Why is this printing 'None' in the output?

Because there are two print statements. First is inside function and second is outside function. When a function doesn't return anything, it implicitly returns None.

Use return statement at end of function to return value.

e.g.:

Return None.

>>> def test1():
... print "In function."
...
>>> a = test1()
In function.
>>> print a
None
>>>
>>> print test1()
In function.
None
>>>
>>> test1()
In function.
>>>

Use return statement

>>> def test():
... return "ACV"
...
>>> print test()
ACV
>>>
>>> a = test()
>>> print a
ACV
>>>

Why does the print function return None?

The print() function returns None. You are printing that return value.

That's because print() has nothing to return; its job is to write the arguments, after converting them to strings, to a file object (which defaults to sys.stdout). But all expressions in Python (including calls) produce a value, so in such cases None is produced.

You appear to confuse printing with returning here. The Python interactive interpreter also prints; it prints the result of expressions run directly in on the prompt, provided they don't produce None:

>>> None
>>> 'some value'
'some value'

The string was echoed (printed) to your terminal, while None was not.

Since print() returns None but writes to the same output (your terminal), the results may look the same, but they are very different actions. I can make print() write to something else, and you won't see anything on the terminal:

>>> from io import StringIO
>>> output = StringIO()
>>> print('Hello world!', file=output)
>>> output.getvalue()
'Hello world!\n'

The print() function call did not produce output on the terminal, and returned None which then was not echoed.

Why is Python code returning 'None' when there is no double print requests?

You want:

start = input('Would you like to find out your USA weather forecast? Please enter YES or NO.\n')

What's happening:

To see what's happening in your code, read it from the innermost function call (print) to the outermost (input). The print function call usually prints something to the screen, but it doesn't return a value, so its default return is None. Then, you call str on that return value. str(None) is "None". Then you pass that value to input, i.e. input("None"). This is what's showing up at your prompt.

Why is None printed after my function's output?

It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.

You probably want to return the values in the function instead of printing them:

def jiskya(x, y):
if x > y:
return y
else:
return x

print(jiskya(2, 3))

Why does this function return None None as well?

Every function always returns a value. If you don't explicitly return a value, and the function just gets all the way to the end, then it automatically returns None. Your function happy doesn't have any return statement, so at the end of the function, it automatically returns None.

Function prints a value but returns none

You missing the return in elif condition

def fib_recur(n, _prev=0, _cur=1, _i=1):
if n <= 1: return n
elif _i <= n: return fib_recur(n, _cur, _cur+_prev, _i+1) # missing return
else: return _prev


Related Topics



Leave a reply



Submit