Why Is This Printing 'None' in the Output

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



Leave a reply



Submit