Student Becomes the Teacher: 8. Part of the Whole


This works, but it isn't in the specified format. I'm don't know how to create a list that allows access to the dictionary.

results = []
for student in [lloyd,alice,tyler]:
    average = sum(results)/len(results)

print results
print average


This is such a list...

Now just assign it to the variable, students.


Don't the names/keys need to be in quotes?


This doesn't work: File "", line 1, in
lst = [lloyd,alice,tyler]
NameError: name 'lloyd' is not defined

students = [lloyd,alice,tyler]
results = []
for student in students:
    average = sum(results)/len(results)

print results
print average


No. They are identifiers. Quotes will make them strings. We need them to point to their respective dictionary objects.

You need the three dictionaries in your code (fill in the missing data).

lloyd = {}
alice = {}
tyler = {}
students = [lloyd, alice, tyler]


It seems like those three lines will overwrite the existing dictionaries, no?

Why doesn't this work? The three dictionaries are part of the script, at the top.

students = [lloyd, alice, tyler]
def get_class_average(students)
	results = []
	for student in students:
    	average = sum(results)/len(results)
    	return average


This is the entire code that produces the answer minus the "get_class_average" function. How do I modify to incorporate the necessary function?

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 get_average(student):
    total = sum(student['homework'])
    homework = total/len(student['homework'])
    total = sum(student['quizzes'])
    quizzes = total/len(student['quizzes'])

    total = sum(student['tests'])
    tests = total/len(student['tests'])

    final = 0.1*homework + 0.3*quizzes + 0.6*tests

    return final

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"
        return "F"

students = [lloyd, alice, tyler]
results = []
for student in students:
    average = sum(results)/len(results)

print average


Define your function, but be sure to not include the list inside. Leave it out.

Instead of calculating the average, use your average function on the return of results.

return average(results)


This question seems flawed on several levels: "class" is a reserved word; average() doesn't exist as a built-in function, but it shouldn't matter because my code calculates average successfully; it's not clear how a list of dictionary names is passed into a function and then used in a for-loop. Frustrated...

Can you present a solution, and explain; your commentary, thus far, has failed to lead me forward.


One of the first functions we write in this exercise is average. The next function we write is get_average which depends upon the average function to do its job. Neither of these functions are built in, just temporarily present in the namespace.

The last function in the sequence is get_class_average which depends upon both of the other two functions.

def average():
    function to return the mean value, or average of a list of numbers
    inputs => numeric list data
    dependencies => none

def get_average():
    function to return a weighted average of averages for one student
    inputs => dictionary containing multiple numeric lists
    dependencies => average

def get_class_average():
    function to return a single mean value with entire class as inputs
    inputs => identifier list pointing to individual dictionaries
    dependencies => get_average, average

The above is the scaffold of what was described earlier.

Note: What is described as class in this exercise refers to a classrom, not a program construct. Eg.

students = []    # class list

The function that takes a dictionary is get_average so let's take a look at that..The parameter student expects an individual dictionary.

def get_average(student):
    average for homework times 0.1
    average for quizzes times 0.3
    average for tests times 0.6
    sum of the above is returned

Unpacking the dicitonary means referring to it by the parameter name.


and so on. You will want the average of all three, using average(). The most common approach, possibly in the instructions being the reason, is to store each average in a variable as the computed amount, then return their sum.

average for homework times 0.1
homework = average(student['homework']) * 0.1


Got it! I went back to square one: I accidentally overwrote my average function, so I replaced it and worked forward. The snippet of code below worked after I put in the average function. Thanks for the detailed explanation.


students =[lloyd,alice,tyler]
def get_class_average(students):
    results = []
    for student in students:
    return average(results)


Added bonus for future study when you get to the unit on advanced topics (don't rush ahead, just come back to this when you finsh that unit)...

This adheres to the documented scaffold of earlier.

def average(g):
    return float(sum(g)) / len(g)

def get_average(s):
    return sum([average(s[k]) * [0.1, 0.3, 0.6][i] \
    for i, k in enumerate(['homework', 'quizzes', 'tests'])])

def get_class_average(c):
    return average([get_average(s) for s in c])

The technique used is called list comprehension, but that's all I'll say for now.


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