# 8: Part of the Whole. What am I doing wrong?

I can’t figure out what I’m doing wrong, the error says, “Oops, try again. get_class_average([alice]) resulted in an error: list indices must be integers, not str”

``````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):
sum (numbers)
total = float(sum(numbers))
a = total / len(numbers)
return a
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
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
def get_class_average(students):
results = []
for item in students :
a = get_average(students)
results.append(a)
return average(results)``````
1 Like

The formating is right (just so you know)

Thanks a lot, but what am I doing wrong?

Here is the error I get with that code:

get_class_average([alice]) resulted in an error: list indices must be integers, not str

Look at what is going on here:

``````    for item in students :
a = get_average(students)
``````

Thanks, but now it says: Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected.

The thing I did was changing the argument after get_average to item instead.

This was the right thing to do. Are you sure you didn’t change anything else?

I’m experiencing the same error message. Not sure how to fix it.

1 Like

FYI - Got mine working. I think the issue stems from assigning get_average function to a single variable, so the return was only calling the variable value. Instead you want to append the functions outputs to the results. So when you return average(results) it is averaging the entire list rather than the b value.

for student in students:
results.append(get_average(student))
return average(results)

1 Like
``````a = get_average(item)
``````

is not assigning the get_average function itself to a single variable, it is assigning the result that is returned by the get_average function. There is nothing wrong with doing that.

A test that you can do yourself:

There is no difference in the value returned between

``````for student in students:
results.append(get_average(student))
return average(results)
``````

and

``````for item in students:
a = get_average(item)
results.append(a)
return average(results)
``````

First of all you seem to be using the “a” variable twice. Once in the average(numbers) function and once in the get_class_average(students) function.

Secondly in the get_class_average(students) function, in the for loop you call the get_average() function on students (which even though we don’t have yet, we expect it to be a list, the list of students more exactly) instead of on the each item (student) on the list.

``````for item in students :
a = get_average(students)
results.append(a)
``````

should be

``````for item in students:
b = get_average(item)
results.append(b)
``````

Then again you can skip using a variable to store the average all together and do

``````for item in students:
results.append(get_average(item))``````

Yeeeeeeeeeeeeeeees, I did it, I’m SO happy.

1 Like

If anyone else is suffering the issue with returning the incorrect average, make sure your “return” line at the end of the script is returning for the “def get_class_average(student):” function, not your “for” loop. You only want to average your results list after the for loop has calculated for all students. By including the return line within the for loop, you begin averaging the results list before all students have their individual averages calculated and added to the list.

tl;dr:
Check your indentation on the final return

6 Likes

After 20+ min of looking on these forums for what I was doing wrong, I saw your post and figured it out! The return average(results) was inside the for loop instead of the function, like you said to check for. Thanks a lot!

2 Likes

I’m having the same issue with not getting the correct average (getting the following error code:
Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected). I have tried changing the indentation of the return line, but then I get a different error code, and I believe that I have it in the correct spot. Here 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]
}

students=[lloyd,alice,tyler]

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”])
sum=0.1homework + 0.3quizzes + 0.6*tests
return sum

if score>=90:
return “A”
elif score>=80:
return “B”
elif score>=70:
return “C”
elif score>=60:
return “D”
else:
return “F”

print result

def get_class_average(student):
results=
for student in students:
x=get_average(student)
results.append(x)
return average(results)
‘’’

Changed to the following and now it works:

‘’’

def get_class_average(student):
result=
for x in student:
result.append(get_average(x))
return average(result)

‘’’

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

this topic is really old, please make a new topic if you have a question.