Printing Grades


#1

For the Python course, we were supposed to print out grades from a list. Here is the code I have: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:
print (i)
return i

print_grades(grades)

What I notice, though, is that while this gives the desired result, when I put instead print(print_grades(grades)), it prints out the last element of grades, 50.5, twice. Why is this?


#2

putting a print statement before the function call will print the returned value by the function.


#3

How come it doesn’t output every number in the list twice then?


#4

because return is outside the loop, so return runs after the loop. i will then contain the last value of list.