Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

paulhmorin Group Title

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)

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. Screech Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • 2 years ago
  2. paulhmorin Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
  3. Screech Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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!!

    • 2 years ago
  4. hanleybrand Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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?

    • 2 years ago
  5. hanleybrand Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    # 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

    • 2 years ago
  6. paulhmorin Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.