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


#1

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]
}
# Add your function below!
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
def get_letter_grade(score):
    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)

#2

The formating is right (just so you know)


#8

Thanks a lot, but what am I doing wrong?


#9

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)

#10

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.


#11

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


#12

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


#13

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)


#14

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)

#15

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))

#16

Yeeeeeeeeeeeeeeees, I did it, I'm SO happy.
:grinning:


#17

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


#18

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!


#19

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.1*homework + 0.3*quizzes + 0.6*tests
return sum

def get_letter_grade(score):
if score>=90:
return "A"
elif score>=80:
return "B"
elif score>=70:
return "C"
elif score>=60:
return "D"
else:
return "F"

result=get_letter_grade(lloyd)
print result

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

Please help, I am stuck!


#20

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)

'''


#21

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


#30

#31

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

No need to include a google drive link, right? Just read this help topic:

that should help you to make indent show on the forum

Posting answers without explanation is not according to the guidelines, @octaviawilliams007 you know that, i told you.