Extra "None" output


#1

https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/printing-out-a-list-item-by-item-in-a-function?action=resume_content_item

So this is my current code:

n = [3, 5, 7]
def print_list(x):
	for i in range(0, len(x)):
		print(x[i])
print(print_list(n))

I get the result:

3
5
7
None

This result was approved, however I don’t understand why there is a “None” printed out at the end.
I tried changing my code to
for i in range(0, len(x)-1)

However, I got this as a result:

3
5
None

I’m assuming this has something to do with how the range function works (which I haven’t gotten to yet).

UPDATE: So I learned what range does, but it still does not explain why the “None” appears at the end. Can anyone explain why this happens?


#2

It might have something to do with printing twice. The print_list function already prints, but you are asking to print the return value from that function, which is non-existent, hence, None.

print_list(n)

See what that gives.


#3

Thank you so much, this makes perfect sense.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.