The code works! But I don't understand why


#1



Python - Student Becomes the Teacher - Part of the Whole


I just completed 'Part of the Whole' after being stuck on it for a while. I finished it by using a line of code that I took from another forum post; specifically line 46:

for student in students:

What I don't understand is how the get_class_average function, well... functions.

From my current understanding, the function begins by creating an empty list, then searching for a variable, 'student', in the argument 'students'. My question is how does the program know what a student is? I don't see where in my code the variable 'student' is defined. Unless it's defined when the get_class_average function calls on the get_average function, which uses 'student' as an argument? But then how is that argument defined?

I'm missing something. I can't seem to trace back where some of the inputs to these arguments are coming from.

Any insight you guys can shed on this would be greatly appreciated!


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))
    return total / len(numbers)

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

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
    else:
        return "F"

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


#2

"Student" is just a place holder. You could use "trout" if you wanted as long as you are consistent.


#3

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