Part Of the Whole - Error


#1

I keep receiving the following error:


I think I've done something wrong in the last function, because everything else has worked this far. 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)
    return float(total) / len(numbers)
   
     
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"
        
get_letter_grade(lloyd)

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

#2

The return average(results) is now part of the for loop, you don't want that :wink:

Just make it like this and it should work:

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

I hope this helps :slightly_smiling:


#3

Great, thank you, you solved my problem!
:slightly_smiling:


#4

I also having same code but facing issue:

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

Error is : Oops, try again. get_class_average([alice]) resulted in an error: 'builtin_function_or_method' object has no attribute 'getitem'


#5

@connecttopawan, Are you still having this problem? can you send your code again and then format it? It'll help to see your indents :slightly_smiling: You can format the code by selecting it and clicking "Preformatted Text"


The little angle tag here :slightly_smiling:


#6

i like your answer, you give an explanation as to why it didn't work as well as the answer. most of the Q+A is just an answer instead of an explanation, which meant i got the answer but didn't actually understand it all the time


#7

I agree. Thanks @sandervb89


#8

Im getting the following error:

"Oops, try again. get_class_average([alice]) resulted in an error: list indices must be integers, not str"

I dont know what to do


#9

@axxotador You might have incorrect indentation like I did? What does your code look like?


#10

@rokaia it looks like this:
def get_class_average(students):
results = []
for student in students:
results.append(get_average(student))
return average(results)

student = [lloyd, alice, tyler]

print get_class_average(student)
print get_letter_grade(students)