Part of the Whole


#1


i get this error
Oops, try again. get_class_average([alice, lloyd]) returned 80.55 instead of 85.85 as expected
what am i doing wrong?


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 = float(sum(numbers))
    result = total / len(numbers)
    return result
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return .10 * average(student["homework"]) + .30 * average(student["quizzes"]) + .60 * average(student["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"
        
def get_class_average(students):
    results = []
    for student in students:
        get_average(student)
    results.append(get_average(student))
    return average(results)


#2

Try converting integers to floats inside get_average as well, for greater accuracy. This looks like you may be losing precision somewhere up the line from the result.


#3

i dont understand....


#4

If you are working with integer object types in get_average(student), it is possible for you to lose precision there, before you get to get_class_average(students)


#5

so how do i fix my code?


#6

Convert any and all integers to float types in get_average or before.


#7

still lost, can you give me an example?


#8

def average(numbers):
    total = float(sum(numbers))
    result = total / len(numbers)
    return float(result)

Something along these lines to ensure precision. That may not be your problem, but it's worth a try.


#9

nopes still not working


#10

I have the same error. I noticed that 80.55 it the average of lloyd alone.

sovled it :slight_smile: put "results.append(get_average(student))" in the for loop.


#11

Thanks so much man! :sweat_smile:


#12

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