Printing Grades


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


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?


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


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


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


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