It's Okay to be Average


#1

I got the code right and passed, but I want to know how my function was able to pull from the dictionaries/lists when it didn't even reference anything there:

loyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}

Add your function below!

def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)

# How does python know that the variable "numbers" would pull from the lists/dictionaries above? This function doesn't mention lloyd/alice/tyler in it at all. Sorry if this is a silly question! Thanks in advance!


#2

average() is utility function that takes a list (of numbers) and computes an arithmetic mean, which it returns. It does not have any need to know what dictionary that list came from.

We can only send one list at a time to this utility. When you get to how the three grade criteria are weighted it will all pull together.


#3

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