Part of the whole


#1

PLZZ HELP ME OUT HERE

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)
total = total/len(numbers)
return total

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"

print get_letter_grade(lloyd)

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

i am getting error

Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected


#2

You might want to add prints in your function(s) which explain to you what it is doing step by step. That way you can tell where it goes wrong and can adjust. Use the input mentioned in Codecademy's error message to call your function since that's what your code behaves incorrectly for.


#3

Hi, You haven't copied code with indentations but I had similar
problem so I assume your code looks like this:

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

and this is incorrect because return is now part of for loop
not whole function get_class_average(). It means that the loop for instead of
doing itself 3 times it does just once and returns value.

It should look like this:

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

#4

Thanks for this :+1:


#5

thanks man you really solved it , i couldn't have found that particular bug thank you so much this will be a lesson so that indentations in future excersices will be correct


#7

Thank you for explaining exactly what was going wrong. Too many times in this forum I find my answer but don't understand why it's the answer. An explanation goes a long way in helping to actually understand and retain this stuff.

You da man.


#8

I am still getting this error after making the ident correction for return. Please help me out.


#9

Hi, did you get the answer?
I had same problem as you.

Turns out you have to create the results [] list within the get_class_average function

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

#10

ccodemmaticc my code looks exactly like yours and is still not working.
What gives?


#11

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

(indented just like yours, of course)


#12

Sorry for the late reply:
After appending the individual student averages in results[]
You need to calculate the average of the items in results[]

Hence you need to return the average of results[] list
Hence
_return average(results)

I hope this helps_