How does this code work?


#1



How is Everybody Doing?


Works but I don't understand how the code knows where to look to get the numbers. How does it know where to look to find the info to fill in student and students?


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


#2

lets break it down, first we have a dictionaries. then we create a list called students which contains the students:

students = [lloyd,alice,tyler]

which now contain your dictionaries, you can see this:

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

now back to your get_class_average function:

def get_class_average(students):
    results = []
    for student in students:
       print student
get_class_average(students)

now we know where students come from, and can see that by looping over the lists, we get access to each individual student.

lets continue:

results.append(get_average(student))

we are appending something to results but what exactly? lets look at get_average:

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

so get_average calculated the weighted average grade for each student, we can see this:

def get_class_average(students):
    results = []
    for student in students:
        x = get_average(student)
        print "the average for", student['name'], "is:", x

so then we append this grades to results, so then we have the following list:

[80.55,91.15,79.9]

then we simply return the average of this lists (we have a function for this, but the math would be:

(80.55 + 91.15 + 79.9) / 3

but our average function calculates this for us, hope this helps


#3

Thanks for this reply! :slight_smile:
How does this code for "student in students:" know what to look up with the student doesn't this need to say name?


#4

not really, student gets assigned each item of the list in turn:

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]

for student in students:
   print student

#5

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