Part of the Whole


#1


The Code works fine although i have no idea why that is.
How does python know students contains the 3 dictionaries of tyler alice and lloyd??

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]
}


# Add your function below!

def average(numbers):
    total = float(sum(numbers)) /len(numbers)
    return total
    
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
    
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
        

def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)


#2

in a way it doesn't, you mean here:

def get_class_average(students):

function parameters like students in this case are placeholder until you call the function, the value you supply at function gets passed along to the function parameter


#3

Hi @simitha,

Your code is correct, as @stetim94 has explained. However, Codecademy lost one of the lines of code that you wrote in an earlier exercise. So that you don't have trouble in a later exercise, see Restoring the missing students list.


#4

To understand why we use students, you must first understand why we use parameters.

As of defining the function, the program does not know whether students is a number, a string, a list, a dictionary, etc. As far as the program is concerned students could be anything and, well, it could care less. It's just a placeholder for whatever you pass into the function when you actually call it. In other words think of parameters like variables. Whenever you call a function, the argument you pass into it becomes the value for students. For instance if you called:

get_class_average([lloyd, alice, tyler]);

You would essentially be telling the program to run get_class_average(students) except now, students = [lloyd, alice, tyler]. Until a function is actually called, the parameters you have set are valueless placeholders. Once you call the function and pass in arguments for those parameters, they take on value and purpose. What I mean by this is, if you did the following:

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]
}

def average(numbers):
    total = float(sum(numbers)) /len(numbers)
    return total
    
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
    
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)

get_class_average([lloyd, alice, tyler]);

Notice how I defined the function using students as a parameter and then I called the function using [lloyd, alice, tyler] as an argument. This is telling the program to run the code within get_class_average(students) but replace all mentions of students with [lloyd, alice, tyler]. Basically it does this:

results = []
for student in [lloyd, alice, tyler]:
    results.append(get_average(student))
return average(results)

This is why we use students as we do in the function.


#5

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