8. Part of the Whole


1) I keep getting this error and I don't get it:
Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected

My code is

Add your function below!

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)
    return total/len(numbers)
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"
        return "F"
    print score

print get_letter_grade(get_average(lloyd))

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

2) A list called "class" is mentioned in the instructions. I don't get it. Isn't "students" the only list I should be making?

3) I've seen other topics and still couldn't understand what I'm supposed to change. One thing that I find weird is that in many answers people are telling to use a "for" loop, like that:

for student in students

How does that work? I've never created a variable, list or dictionary called "student". How is the program suddenly going to understand that?


Where is your get_class_average function? Codecademy's message refers to it, but it is not posted.


There it is. Had some trouble editing it.


Ah, I see it now. Sorry for the oversight.

This statement should be outside the function, rather than within it ...

    students = [lloyd, alice, tyler]

You need to loop through the students list in that function. Don't assume that students will always refer to exactly three items.

(EDITED to correct a spelling error)


Did it and got that:

Oops, try again. get_class_average([alice]) resulted in an error: list index out of range


That is because when Codecademy calls the function with the list, [alice], your code still assumes that the list has three items in it. Try a for loop with the header ...

    for student in students:

Then student will represent each item in the students list within the loop.


How exactly should I loop? I know I should use for, but I don't know how.


How does student represent each item in the students list? I haven't created anything with that name.


Inside the loop, below the header, you can do this ...


As you refine your get_class_average function, post it again for us to see.


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

Still getting the same error.


Move this statement to outside the function ...

students = [lloyd, alice, tyler]

Otherwise, you are defeating the purpose of the students parameter. The function needs to be able to process lists other than [lloyd, alice, tyler] when it is called.


Now I got it right! Thank you very much!