anonymous
  • 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)
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • 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.
anonymous
  • anonymous
here is what I get when I print bar after bar = average
anonymous
  • 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!!

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • 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?
anonymous
  • 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
anonymous
  • anonymous
Thanks for your help. Hate it when programmers depend on "hidden" functions. Your code is self explanatory. bar = average is not.

Looking for something else?

Not the answer you are looking for? Search for more explanations.