Part of the Whole


#1

https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

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


i dont know why i get this error


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

def get_class_average(students):
    results = []   
    aclass = [lloyd,alice,tyler]
    for student in aclass:
        result = get_average(student)
        results.append(result)
    return average(results)


#2

you overwrite your function parameter. you shouldn't.

you can supply aclass as argument when calling the function, but define aclass (which should be named students actually) outside of your function


#3

did not quite get what you intended
i moved the aclass list outside the function but still got the same error


#4

can i see an updated version of your code?


#5

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

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(get_average(lloyd))

aclass = [lloyd, alice, tyler]

def get_class_average(students):
results = []

for student in aclass:
    result = get_average(student)
    results.append(result)
return average(results)

#6

you shouldn't loop over aclass:

for student in aclass:

your function should work for a class of any size (1, 2 or 3 students)


#7

how do i do it?
i thought for loop is for each item in the aclass list


#8

look:

def get_class_average(students):
    results = []   

    for student in aclass:
        result = get_average(student)
        results.append(result)
    return average(results)

print get_class_average([lloyd])
print get_class_average([lloyd, alice])
print get_class_average([lloyd, alice, tyler])

your function should work for all this cases, you should do something with your function parameter


#9

i dont see any problem
i have no idea whats wrong


#10

all this function calls give the same result, while they shouldn't


#11

i know! but i cant see why!

for every student in my aclaas list i calculate his avg and then i put it in a results list and then i do avg on that list
so if i put lloyd it should calculate only lloyd but it doesnt and im clueless on why it does it


#12

because you are looping over aclass? don't manipluate aclass, the code should work as i presented here:

def get_class_average(students):
    results = []   

    for student in aclass:
        result = get_average(student)
        results.append(result)
    return average(results)

print get_class_average([lloyd])
print get_class_average([lloyd, alice])
print get_class_average([lloyd, alice, tyler])

when calling the function you supply argument(s), which get copied into the function parameter, so maybe you can use the function parameter?


#13

thank you got the problem


#14