Why does my print_grades( ) not work?

Finally got it, I thought a function call needed to be ‘returned’ rather than just writing it by itself.

I’m late to the party, but the answer to your question is:
The function print_grades already prints out the list. That is what you see in the console. Your final line of code is printing out the RETURN of the function. Since there is no “return” in your function, it returns the default “None”.

remove (), just leave print i

What is the reason as to why these bits of code come out differently?

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def print_grades(grades_input):
for i in grades_input:
return i
print (print_grades(grades)) #this print out 100 ONLY

def print_grades(grades_input):
for i in grades_input:
print (i)
print_grades(grades) #this prints the whole list

Since 100 is the first value in the list it is the current value of i. The next line returns it. End of story. The loop never gets to run out the full distance.

I don’t know why I’ve never considered it/find this so counter intuitive, but why wouldn’t the loop in the first example just go to the next index in the list and return " i " when we call the function with print? I’m just struggling to understand why the for loop just stops after the first index because of a return inside the loop.

return is final. All the states that were present are gone. Whatever expression is returned is all the caller will ever get. The function itself is exited. There is no going back.

This is news to me. Good to know! Thanks for the info!

1 Like

My question now is, what is your function expected to return to the caller? That is, what is the caller expecting in return? A list, maybe? Or, None?

If I am understanding your question correctly, my goal was to return each index on a separate line in the list.

That goal is unachievable since we can only return from a function once (unless recursion is in play, which is not the case here, by a long shot).

The name of the function is print_grades so one suspects there is no expected return and the function is to go through the list it is given and print them out. No return involved. This is an action only type function that has no effect on the data it is given. It just has to iterate it, and print it.

At least that’s how I interpret the name of the function. Are we thinking along the same lines when that comes into view?

Yes are along the same lines.

1 Like

So now the second example in you earlier post makes sense?

Yes, after clearing up the how the “return” command in the first example works, the second example clear as to why it would continue to iterate through the loop.

Very good. This is a lesson to keep front and center when it comes to where return belongs in a function. It has nothing to do with print ().


There is a common belief among learners that print and return are the same, or nearly the same thing. That is a complete myth and categorically untrue. They are nowhere near the same thing.

print() is a function. That means it executes when called and as it were, gives nothing back but, None, which will be the return value of the function if it reaches its end.

return is a keyword that tells the interpreter to pass the included expression back to the caller.

return expression

A return value is assignable or usable in another expression. Can’t think of too many ways that None is useful in any expression, save a boolean.

Instead of writing this:

You can try just:print_grades(grades)

Yes, the print_grades function prints out the desired output since each grade is not being returned, but printed.

However, it’s also important to understand why None is being printed. The default return value for a function is None. In other words, if you don’t specify something to return, None will be returned. By using print on print_grades, you are printing what is being returned from that function, in this case None.