Python-3: Why This Following Code Returns None in Print Statement

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 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 am i getting none at the end of my output in the following code and how to avoid it?

Your function doesn't have a return value, meaning it implicitly returns None. To avoid that, either don't wrap the function call in a print statement or return the value you intended to print inside the function

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.

Python function returns none after 3 rd use of None

cc gets called twice here.

print cc(klds(asa())), equivalent to print cc(7) is the first time it gets called. Trace it through:

if k != 7:  # Nope, keep going...
print 2
rt(k+2)

We've hit the end of the function. There is no return statement, so it return None, which is what you're seeing at the end.

To fix it, make sure you're returning your values. (i.e. return rt(k+2) instead.)

There is a similar problem elsewhere in your code you'll also have to fix before it works like you want it to.

why do i got 3 times the output none in my code?

You are printing the results of y_n(), which is None try this:

def name_tag():
name = input("Hello, whats your name? : ")
print(f'Hi {name}, would you like to play a game?')
y_n()

def y_n():
yes_no = input("Y/N: ")

if yes_no.lower() == 'y':
print('Cool, than lets play a game')

elif yes_no.lower() == 'n':
print('To bad!')

else:
print('Please anwser with [y] or [n]')
y_n()

If you want to use print(y_n()) you could change it so you are returning the strings from y_n():

def name_tag():
name = input("Hello, whats your name? : ")
print(f'Hi {name}, would you like to play a game?')
print(y_n())

def y_n():
yes_no = input("Y/N: ")

if yes_no.lower() == 'y':
return 'Cool, than lets play a game'

elif yes_no.lower() == 'n':
return 'To bad!'

else:
print('Please anwser with [y] or [n]')
return y_n()


Related Topics



Leave a reply



Submit