2/9 Print those grades


#1

I thought only this worked,

#Instructions

#Define a function on line 3 called print_grades() with one argument, a list called grades.

#Inside the function, iterate through grades and print each item on its own line.

#After your function, call print_grades() with the grades list as the parameter.

#Your Code!

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades):
    for grade in grades:
        print grade
print print_grades(grades)    # Notice the 'print'

but why does this also work? :frowning:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades):
    for grade in grades:
        print grade
print_grades(grades)          # There is no 'print'

#2

doesn't matter, both will works since there is a print inside the function. If you would use a return, thing change. Then the first one would print, and the second one would not


#3
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades):
    for grade in grades:
        print grade
print_grades(grades)          # There is no 'print'

The code above works just as well as the first one because inside your print_grades(grades) function, you are doing "print grade". So it'll do it anyway.


#4

So for this one, you are actually printing the result of calling your function print_grades.

and now for this one, you are just calling your print_grades() function which already prints what you want printed because you told it to.

Hope this helps :slight_smile:


#5

Hi, @josephpoon ,

Your code demonstrates an interesting issue.

The print command displays a representation of the value of expressions that are specified after the word print. Since your print_grades function does not contain a return statement, it returns the value, None. Therefore, the print command in this line of code displays None ...

print print_grades(grades)

However, since it calls the print_grades function, the statements inside the function, including the print command that it contains, are executed, and it displays sucessive values of the variable, grade, from inside the for loop. Then, after that, the value, None is returned and displayed.

The outcome is that first you should see values of items that are contained inside the grades list. Then, you should see the value that is returned, namely, None. Since the incidental value of None that the function returns is really not of much interest, the best means of using your function is to call it without invoking the print command, as follows, just as you did in your second code example ...

print_grades(grades)

#6

Any idea why this is not accepted even if the outcome is correct?

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades):
    print('\n'.join([str(grade) for grade in grades]))
    return None

print_grades(grades)

#7

Hi @id2205 ,

Codecademy checks for output of each item as an individual string. Your submission prints them all together as one string with embedded \n characters, and Codecademy did not like that. However, you did print each grade on its own line, so if I were your instructor, I would have passed the code that you submitted.