How did python know where to look for the keys?


#1



https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/5?curriculum_id=4f89dab3d788890003000096


No errors -- I just have a question about how Python knows how to look up some of the values in the code.


Looking at the def_average function, I am trying to understand how this line works:

    homework = average(student["homework"])

student is an argument that was introduced for the first time when defining this function. The 3 dictionaries (lloyd, alice, and tyler) don't include any reference to student. I realize I could have named the argument anything else, and student was an arbitrary choice. I just don't know understand how python knew to look through all of the dictionaries in the preceding code in order to find any keys == homework.

Does this mean that python will always scan through all preceding code in order to find matching keys?

Or was there something going on behind the scenes in this codecademy demonstration?

I've posted my entire (working) code below for reference

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 = sum(numbers)
    total = float(total) 
    result = total / len(numbers)
    return result

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)

print get_average(alice)
print get_average(lloyd)
print get_average(tyler)

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)

print get_class_average(students)
print get_letter_grade(get_class_average(students))


#2

here:

def get_average(student):

student is the function parameter, which acts as a placeholder until you call the function:

print get_average(alice)
print get_average(lloyd)
print get_average(tyler)

where you supply an argument to satisfy the function parameter


#3

OOOOOOOOOOkay, I think I get it.
The function isn't actually doing anything until I call it and supply the actual key for looking up the data.

I see the same thing is happening with the earlier average function: numbers is a placeholder.

So when I get_average(alice), it's looking up the sum() of values in alice's homework, quizzes, and tests, converting to float (which doesn't seem necessary since they were careful to tell us to include decimals?), dividing by the total number of values in each category, then weighting each of those according to their values...

I get it. Instead of building one function that does all of this (which would probably be complicated), we built several small functions to handle each part of the process.

Neat!

I'm a teacher, so this is actually something I can see myself doing this semester.
Bravo, codecademy!


#4

You seem to understand it perfectly.

Making a single function wouldn't be very practical, the function are designed in such a way, that each task can be accomplished separately. (get average grade of a single student, for multiply student (get_class_average), convert to letter grade when you want


#5

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