#1

Just Weighted and See

for the code below, it shows get_average(alice) raised the following error: string indices must be integers, not str

def get_average(students):
for student in students:
homework = average(student[“homework”])
quizzes = average(student[“quizzes”])
tests = average(student[“tests”])
return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

while the correct code still have str in get_average, why is that

def get_average(student):
homework = average(student[“homework”])
quizzes = average(student[“quizzes”])
tests = average(student[“tests”])

total = homework *.1 + quizzes * .3 + tests * .6

#2

`students` is a list of identifiers. `student` is a dictionary. That will make all the difference. `get_average` is intended to return a single average for one student.

#3

so if I intend to return the average for all student, can I use a list of student, like students?

#4

If you were to iterate over the list, and call `get_average` on each one, it would make perfect sense.

``````student_averages = {}
for student in students:
student_averages[student['name']] = get_average(student)

print student_averages
``````
``````{'Tyler': 79.89999999999999, 'Lloyd': 80.55, 'Alice': 91.14999999999999}
``````

That’s just an example, not solution code.

#5

got it, thank you so much

#6

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