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 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
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 is my Python script printing None after using a function and using input(print())?
You are mixing a few things here.
In order for you to print the input of the user, you must first get it. You did it correctly with the input()
function, but this will return you the result. That's what you need to print.
So you should change your code from this:
input(print("\nis this correct?\n[y]/[n] (yes or no): "))
to this
print(input("\nis this correct?\n[y]/[n] (yes or no): "))
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 my code printing 'None'?
Seems likes story.print_story()
does not return anything.
If a function does not return, it returns None
.
>>> def a():
... print 1
...
>>> print a()
1
None
Remove print
from print story.print_story()
.
Related Topics
Count the Number of Occurrences of a Character in a String
Can a Variable Number of Arguments Be Passed to a Function
"Ask Forgiveness Not Permission" - Explain
What Is the Formal Difference Between "Print" and "Return"
Python Function Global Variables
How to Get Json to Load into an Ordereddict
How to Replace Nan Values by Zeroes in a Column of a Pandas Dataframe
Is There a Simple, Elegant Way to Define Singletons
Pretty-Print a Numpy Array Without Scientific Notation and With Given Precision
Is There a Zip-Like Function That Pads to Longest Length
How to Create a New Column from the Output of Pandas Groupby().Sum()
How to Install Python Packages [Ssl: Tlsv1_Alert_Protocol_Version]
How to Add New Keys to a Dictionary
How to Pass a String into Subprocess.Popen (Using the Stdin Argument)
How to Make One Python File Run Another