Confused on Part of the Whole


#22

In the exercise it says to use the argument students, which you should expect to be a list containing 3 students. As to why putting non-sense as the argument I'm not sure. TBH I may be in over my head there. I assume the argument would work as students so that my for loop would loop through the list of students and calculate the "class average". If you're hinting at what I think you are: you're saying Ive messed up that line of logic somewhere and no matter what argument I put it would spill the same thing out.

As far the global variable change. I changed it because it seemed to coincide with my for loop

for students in students

as stupid as that sounds. And it very well may be. I'm pretty new to this and trying to abstract think this is difficult right now.


#23

The students that the function should compute an average for are supplied in the argument.

You were doing this:

def add(a, b):
    return c

The result had nothing to do with the arguments.

Your function has no legitimate interest in that global variable.


#24

so where was it returning c from?


#25

Since c isn't in that function, it would look outside the function. If there's such a global, then that would be used. If there's no such global there would be a NameError

def add(a, b):
    return c

c = 3

print add(2, 2)    # 3
print add(1, 3)    # 3
print add(1000, 2) # 3
print add(2, 45)   # 3
print add('purple', 'rain')   # 3

^ same behaviour as your function had


#26

hence why it pulled from the global correct?


#27

Ok. So how should I go about properly fixing this?


#28

Just use local variables.

def add(a, b):
    return a + b  # a and b are defined locally, won't look outside

a, b = 2, 8

print add(7, 8)  # 15

Python has no way of enforcing that you do this. And indeed you're still going to want to use functions which aren't local (that's just variables referring to function objects)


#29

Ok so let me try to wrap around this one more time.

return average(results)

this return is looking outside my function?

from what i thought:

the average() would be using results which I defined in my function already. So where and why would it be pulling globally?


#30

Depends on whether it finds them locally or not. average is probably non-local. But it's a function, that's fine. Point is that it shouldn't be pulling information from arbitrary locations

If I do add(5, 7) then guess what, I expect 12 as a result


#31

Yeah I get what you're saying with this

5 + 7 = 12 thing.

If you don't mind. Can you point out in my "passing" code where the breakdowns are. Because I need to sleep I think. When I wake up I'll take another look and try to rethink this problem. Because right now I'm circling in my head and that's not helping me or you. If the problem mainly lies in my last function then perhaps I should start from scratch on the get_class_average function.

possibile dumb question inbound:

would it work to

use previous function that calculate say lloyd's average and use that for all 3. then use simple math in python to define "class average"?

double edit: i now realize what i described is exactly what this last function is supposed to do...oh boy


#32

No clue what you mean with that though.

But it all just boils down to that most functions should be isolated from their surroundings. Don't get or set globals, operate only on the arguments.


#33

No clue what you mean with that though.

But it all just boils down to that most functions should be isolated from their surroundings. Don't get or set globals, operate only on the arguments.
They should be viewed as black boxes that you can't break, they just do their thing every single time without fail. You should be able to read it and say "yes, that is correct", which you can't if it relies on global values.


#34

This sort of high-level theoretical questioning as opposed to help is not useful to help beginners get excited about learning to code, which is really what this site is for


#35

If by help you mean "edit these two lines exactly like this and don't even bother trying to understand how I got to that conclusion"

then uhm.. no. That's how to make it impossible to reason about upcoming concepts due to standing on a two-legged chair. I'm gonna provide a few more legs for that chair instead.
But thank you for the advice.


#36

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