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
Regexp Finding Longest Common Prefix of Two Strings
How to Make Good Reproducible Pandas Examples
How to Pass a Variable by Reference
Using Global Variables in a Function
Why Does "A == X or Y or Z" Always Evaluate to True
How to Select Rows from a Dataframe Based on Column Values
How to Sort a Dictionary by Value
Why Is Using 'Eval' a Bad Practice
How to Iterate Over Rows in a Dataframe in Pandas
Pandas Conditional Creation of a Series/Dataframe Column
Does Python Have a Ternary Conditional Operator
Unboundlocalerror on Local Variable When Reassigned After First Use
Converting String "Jun 1 2005 1:33Pm" into Datetime