Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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)
See more answers at brainly.com
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.

Join Brainly to access

this expert answer

SEE EXPERT ANSWER

To see the expert answer you'll need to create a free account at Brainly

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.
here is what I get when I print bar after bar = average
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!!

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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

Not the answer you are looking for?

Search for more explanations.

Ask your own question