8. Part of the Whole


#1



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"
    else:
        return "F"
    print score

print get_letter_grade(get_average(lloyd))


def get_class_average(students):
    students = [lloyd, alice, tyler]
    results = []
    results.append(get_average(students[0]))
    results.append(get_average(students[1]))
    results.append(get_average(students[2]))
    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?


#2

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


#3

There it is. Had some trouble editing it.


#4

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)


#5

Did it and got that:

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


#6

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.


#7

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


#8

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


#9

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

        results.append(get_average(student))

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


#10

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

Still getting the same error.


#11

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.


#12

Now I got it right! Thank you very much!


#13