8.Part of the Whole - Code is correct but it won't run


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

The returned error message is "Oops, try again. get_class_average([alice]) resulted in an error: list indices must be integers, not str"

I went through the code again and again but I still can't fined the issue.


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)
    result = total/len(numbers)
    return result
    
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 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
    else:
        return "F"

fun = get_average(lloyd)
print get_letter_grade(fun)

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


#2

here:

results.append(get_average(students))

students is a list with students, get_average only works for a single student, that is why you have the loop, to get the average of each student, append it to results list


#3

I did that, it still gave me the same error message.


#4

you use students here, which is the list, not the individual student.

Yes, you use student here:

get_average(students)

but this is not what you append to the list in the end


#5

Ok, what do I append?


#6

well, you have two lines:

        get_average(students)
        results.append(get_average(students))

the first one gets the correct average, but doesn't append it to results

the second line tries to get the average of students list, but get_average can't handle a list

If you fix first and remove second or vice versa doesn't matter. Just make you append average grade of student (not studentS list) to results list


#8

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