6.Just Weight And See


#1

This is my code:

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)
avg=total/len(numbers)
return avg
numbers=[2,3,4,5,6]
average(numbers)

def get_average(student):
homework=average(student["homework"])
quizzes=average(student["quizzes"])
tests=average(student["tests"])
total=(0.1*homework)+(0.3*quizzes)+(0.6*tests)
return total

student=[lloyd,alice,tyler]
get_average(student)

I am getting this error:
Traceback (most recent call last):
File "python", line 38, in
File "python", line 31, in get_average
TypeError: list indices must be integers, not str

Please help me resolve this problem


#2

is expecting a dictionary but is instead seeing a list. This suggests that we might either iterate the list, or access each element directly:

students = [ lloyd, alice, tyler ]
get_average(students[0])            # ( lloyd )
get_average(students[1])            # ( alice )
get_average(students[2])            # ( tyler )

But we don't need a list just for this. The upcoming exercise will explore iteration of the list of references.