Error calculating get_clas_Average - Part of the Whole


#1



8. Part of the Whole


The code bellow runs well and alice average is 91.15. I added some prints to see alice's average.

When running the code a get the following error message: Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected

Any help?


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]
}

class_list = [lloyd, alice, tyler]

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    total / len(numbers)
    return total / len(numbers)
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return homework * 0.1 + quizzes * 0.3 + tests * 0.6
    
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 get_letter_grade(get_average(lloyd))

print average(alice["homework"])
print average(alice["quizzes"])
print average(alice["tests"])
print get_average(alice)

def get_class_average(students):
    results = []
    for student in class_list:
        results.append(get_average(student))
    return average(results)


#2

Use the parameter variable, not class_list:

for student in students:

#3

Hi it worked!

But now I got confused... I have not define any list or variable named students... so how does the for loop know where to get the names to run the function?


#4

Back in one of the earlier lessons we created a students list, but for some reason it doesn't carry forward. Just go back up to the data and write it under the dictionaries:

students = [lloyd, alice, tyler]

Be sure to not quote those names. They are identifiers.


#5

Correct!

That is why I created a different list.

Thank you for your help


#6

Just to clarify, you could use any name in your function for the class list, since it is a local variable.

def get_class_average(class_list):
    # code

get_class_average(students)

#7

The exercise is complete, but can you explain me why the calculation was wrong with my initial code?

When I had a class_list defined?

Thank you once more


#8

It has to do with the parameter. Your code would have worked fine without a parameter, since class_list is a global object. But the SCT is passing in a list, which your code was ignoring in favor of the global object. It was unable to pass the SCT check as a result.


#9

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