Print those grades 2/19


#1

I’ve had some difficulty with this exercise. Finally I’ve gotten the below code to pass me but dont understand why it would without the grades_input being first assigned to grades. Can some one explain please?

Thanks in advance


#2

When you call a function you are telling it what data to operate on

add(3, 4)  # I expect this to return 7

#3

Thanks for the response. I understand that, but what I dont get is why the program will automatically equate the argument (the list) grades_input to grades the list [100, 100, 90, 40, …etc], without it being specifically told this. What am I missing here. I’d really appreciate your response.


#4

You’re sending that list as input when you call your function

def add(a, b):
    return a + b

add(3, 4)

#5

Never thought of it that way before. I had always thought that each ongoing step had to be directly relat-able to the one preceding it, such that for instance we should have been told somewhere along the line before, that the argument (the list) grades_input equates to grades the list [100, 100, 90, 40, …etc]. Thanks for the explanation. I think I get it now (And I’d really have to try to remember this one for the future!)


#6

That’s what a function call does. It sets up the arguments (and calls the function)


#7

Here’s your code, and below it, the bytecode which is created from that code, and is executed by python

Line 9 is the function call. Look at the bottom left of the byte-code where it says 9
It loads the function name at (54), then the argument at (57), then calls the function at (60)

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


def print_grades(grades_input):
    for item in grades_input:
        print item


print_grades(grades)
  1           0 LOAD_CONST               0 (100)
              3 LOAD_CONST               0 (100)
              6 LOAD_CONST               1 (90)
              9 LOAD_CONST               2 (40)
             12 LOAD_CONST               3 (80)
             15 LOAD_CONST               0 (100)
             18 LOAD_CONST               4 (85)
             21 LOAD_CONST               5 (70)
             24 LOAD_CONST               1 (90)
             27 LOAD_CONST               6 (65)
             30 LOAD_CONST               1 (90)
             33 LOAD_CONST               4 (85)
             36 LOAD_CONST               7 (50.5)
             39 BUILD_LIST              13
             42 STORE_NAME               0 (grades)

  4          45 LOAD_CONST               8 (<code object print_grades at 0x7f61cf01b730, file "s.py", line 4>)
             48 MAKE_FUNCTION            0
             51 STORE_NAME               1 (print_grades)

  9          54 LOAD_NAME                1 (print_grades)
             57 LOAD_NAME                0 (grades)
             60 CALL_FUNCTION            1
             63 POP_TOP
             64 LOAD_CONST               9 (None)
             67 RETURN_VALUE

#8

If you’ve discussed functions in math class, you’ve seen something like this:

f(x) = x + 1

And then to evaluate that function you’d do:

f(3) + 5
(result: 9)

Nowhere does that say x = 3, but the function call says that the first argument is 3, and the function itself says that the first argument is x


#9

Thanks for the explanation


#10

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