Why Is the Output of My Function Printing Out "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 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 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))

Python printing None in result , Why?

first_student.show_student() returns None, which is what all Python functions return if no explicit return value was specified. Therefore:

print(first_student.show_student())

This will evaluate to print(None).

Why in this program print None in output?

You need to remove the print when you're calling your fuction since you're already printing the result inside it.

Code:

def skip_elements(elements):

for index in range(len(elements)):
if index % 2 != 1:
print(elements[index],end=" ")

skip_elements(["a", "b", "c", "d", "e", "f","g"])
skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])

Output:

a c e g Orange Strawberry Peach 

How do I prevent returning None between desired output?

I think you meant to write your debug decorator like this:
(properly indented)

def debug(func):
"""
:param func: function
"""
def inner_func(*args,**kwargs):
answer = func(*args,**kwargs)
print(f"{func.__name__}{args} was called and returned {answer}")
return answer

return inner_func

You meant to return answer rather than the result of a print() which is always None.

Also, if you don't want to see the returned answer, then don't print it. Since the decorator calls print(), just call the functions:

add(3, 4)
sub(3)
sub(3,4)

Also, in particular, if you want this line:

sub(3) was called and returned -97

you can change the print line to this:

print(f"{func.__name__}({','.join([str(arg) for arg in args])}) was called and returned {answer}")


Related Topics



Leave a reply



Submit