Just Weight and See - Question


I posted this question already, but a member of staff decided that I didn't deserve an answer so they deleted my question......(I'll admit I got angry cos I couldn't believe that they said I must have copy and pasted the code and I didn't deserve an answer and I may have used profanity cos I spent ages working out the exercise so FAIR ENOUGH) Anyway I'm just gonna ask it again cos I really am pretty confused by this:

I typed this code and it works for the section. I understand the structure, but theres one thing I don't understand: How does Python know what I mean when I use the word average(numbers) - even though I've never defined what numbers is? And the same way, how does it know what I mean by get_average(student), even though I never told it what student is? Does it store it automatically? Am I forgetting and not applying a principle from earlier in the course?

Thank you in advance! My code:

lloyd = {
"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]

students = [lloyd, alice, tyler]

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

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return 0.1 * homework + 0.3 * quizzes + 0.6 * tests


Hi @rokaia ,

Yes, remember to be polite on the forums. All of us sometimes get frustrated when we are having trouble solving a problem, but if we're not nice to each other, the forums will not feel like an inviting place to go.

Because the code that you posted is not formatted, it is difficult for other users to read and debug it. After code has been pasted into the editing window for posting, you can format it by selecting it, and then by clicking the </> button above the editing area. Alternatively, you can place three backquotes on the line before the code and three backquotes on the line after the code. This will enable us to see important details, such as the indentation and underscores. If you use the backquotes, your code will be color-coded, making it especially easy to read.

Here's what you have for the average function ...

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

... but when we format it using the three back ticks before and after the code, it looks much better in the post ...

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

Let's say you have a list, defined as follows ...

grades = [90.0, 85.4, 78.9, 99.0]

Then, since you defined the average function, you can do this ...

print average(grades)

Now, the Python interpreter has been informed that a reference should be passed from the variable, grades, to the parameter, numbers. Then, as the average function executes, the values in the grades list are referenced by the name, numbers.

One thing that is nice about functions is that you can re-use them. For example, later you can do this ...

scores = [88.9, 91.1, 91.7, 90.1]
print average(scores)

Now, the name, numbers, will refer to the same list as scores does, within the function, as it executes.


Great, thank you for your help!