Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 63 Online
OpenStudy (anonymous):

In the following code, HOW can the "average" function work with no visible input passed to it? How does it know where "stuff" points to? The code works I just don't know why. def average(stuff): return sum(stuff)/len(stuff) def getAverage(kid): bar = average return bar(kid["homework"])*.1 + bar(kid["quizzes"])*.3 + bar(kid["tests"])*.6 studavg = getAverage(x)

OpenStudy (anonymous):

Its not a good programming technique (at least IMHO), but the line bar = average creates an alias for the average function within getAverage(). My guess is that the formula used specified variables with a bar over the top, which in statistics traditionally means average or mean. Its pretty confusing, but within getAverage(), bar and average mean exactly the same thing.

OpenStudy (anonymous):

here is what I get when I print bar after bar = average <function average at 0x7fbc6280bc80>

OpenStudy (anonymous):

Sounds likely. You'd probably get the exact same output from print average So, when getAverage invokes bar(...), its just as if it had invoked average() Why make it so confusing? Saves typing 4 characters. pfffft!!

OpenStudy (anonymous):

How it works - first "stuff" needs to be an iterable object (the obvious one with this code is dict), so when the line studavg = getAverage(x) is interpreted, x needs to be a dict (so getAverage can use the key (e.g. "homework") to pick the correct answer - and this brings up an additional point, it only makes sense if x is is a dict of lists. I'm going to change x to scoreDict for accuracy: studentScores = { "homework" : [25, 50, 75], "quizzes" : [25, 75], "tests" : [25,50,75,100] } def average(scoreList): return sum(scoreList) / len(scoreList) # fix getAverage for clarity def getAverage(scoreDict): h = kid["homework"] q = kid["quizzes"] t = kid["tests"] return average(h) * .1 + average(q) * .3 + average(t) * .6 print getAverage(studentScores) # 57.2 So average() does have visible input being passed to it - getAverage() invokes it 3 times, once for each key in studentScores. Is that any clearer?

OpenStudy (anonymous):

# Sorry, I pasted in the unfinished fix - this will run without error: studentScores = {"homework": [25, 50, 75], "quizzes": [25, 75], "tests": [25, 50, 75, 100]} def average(scoreList): return sum(scoreList) / len(scoreList) # fix getAverage for clarity def getAverage(scoreDict): h = scoreDict["homework"] q = scoreDict["quizzes"] t = scoreDict["tests"] return average(h) * .1 + average(q) * .3 + average(t) * .6 print getAverage(studentScores) # 57.2

OpenStudy (anonymous):

Thanks for your help. Hate it when programmers depend on "hidden" functions. Your code is self explanatory. bar = average is not.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!