11.8 Part f the Whole


#1

im taking tihs error: Oops, try again. get_class_average([alice]) resulted in an error: 'dict' object has no attribute 'append'
and coudlnt find the mistake its not making any sense to me
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]

Add your function below!

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

def get_average(student):
return (average(student["homework"])10)/100 + (average(student["quizzes"])30)/100 + (average(student["tests"])*60)/100

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(student):
results={}
for student in students:
results.append(get_average(student))
return average(results)


#2

The problem is n this line

results.append(get_average(student))

you cannot append to a dictionary.The append method does not exist for dictionaries.
You created results to be a dictionary

results={}

to fix this you need to rather make results a list

results=[]

#3

omg i want to kiss you thanks mate


#4

do you have any idea about tihs:

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


#5

People usually get this error because of indentation. Make sure all your indents are right and you are calling the return keyword outside the for loop

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

#6

they looks like true


#7

try this. i commented on what changed

#changed the parameter to 'students'
def get_class_average(students): 
    results=[]
    for student in students: 
        results.append(get_average(student))
    return average(results)

#8

yeah i just found this in 3 month before q&a about to try it , it worked thank you very much i was about to loose my mind


#9

When I try your code this is the error I get: Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected


#10

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


#11

students = [lloyd, alice, tyler] should be outside the function


#12

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

this works..


#13

My code works even without the students list, but I'm not sure why. They only code I did differently than original was the get_average(student) by using a for loop:

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

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    for avg in student:
        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(get_average(lloyd))

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

print get_class_average(student)
print get_letter_grade(get_class_average(student))

Also thought I'd note it doesn't seem to care if I have it as

print get_class_average(student)

or

print get_class_average(students)

even if the next line doesn't match singular or plural