8. Part of the Whole: Bad Average?


#1




Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected


I expected it to return the proper average because if I print my get_class_average(students) function, it returns the correct average which I confirmed on a calculator. My return is outside of the for loop on the last function...seems like I'm stuck!


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 (homework * .10) + (quizzes * .30) + (tests * .60)
    
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"

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


#2

@trisblank ,

This should be global, rather than inside the get_class_average function ...

students = [lloyd, alice, tyler]

As you have in now, the get_class_average function can only compute results for a class that contains lloyd, alice, and tyler. It needs to be more general than that.


#3

@appylpye

Thanks! That did it. Can you by chance explain why the global list is more appropriate? I'm guessing since the average was actually correct it wasn't 'wrong' in the sense of a math problem but it was wrong in the context or proper syntax etc.


#4

The syntax that you had was fine, but the logic of the get_class_average was faulty. With this statement inside the function, instead of outside it ...

students = [lloyd, alice, tyler]

... the function would process data for a class consisting of lloyd, alice, and tyler, regardless of whether a different list of students were passed to it via the students parameter that is specified in the function header. In fact, Codecademy tested your function for a class consisting only of alice. When your function returned a result that was appropriate for lloyd, alice, and tyler, instead, it failed that test.


#5

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