# Just Wait and See - get_average loop for all 3 students

#1

Hello, I'm trying to modify the get_average function so that it will loop through all three of the student's dictionaries and return the weighted average result for each one. However, I'm having trouble and I'm not sure why.

I keep getting a " get_average(alice) raised the following error: string indices must be integers, not str" error. Any help would be greatly appreciated.

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

def average(numbers):
total = sum(numbers)
total = float(total)

nums = alice["homework"]
print average(nums)

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

stud = [lloyd, alice, tyler]
print get_average(stud)``````

#2

my code is quite similar to yours, but without for loop. and it works

def average(numbers):
total= sum(numbers)
total = float(total)

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

#3

You don't need this for loop.

#4

In my opinion, there hardly is a need for a `for` loop just as @megajumper27894 said. That said, there are many ways to kill the question.

Use @megajumper27894 's code to help :

``````def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return 0.10*homework + 0.30*quizzes + 0.60*tests``````

#5

define student dictionary :

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

write functions :

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

def get_average(student):
homework=average(student["homework"])
quizzes=average(student["quizzes"])
tests=average(student["tests"])
return (1/10) * homework + (3/10) * quizzes + (3/5) * tests

how do i pass the student dictionaries as parameters to the get_average function?
do i need to rewrite the function?
what am i doing wrong?

#6

Your get_average function looks good, use print statement to call the function get_average() with each students.
Hope it will help

#7

Hello ktsui. thank you very much. i managed to get the code to compile and run.

#8

try this for looping through the dictionary

stud = [lloyd, alice, tyler]
for student in stud:
print get_average(student)