Part of the Whole returning wrong number


#1



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


My code keeps bringing up the following error message: "Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected"


Not sure why this is happening


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):
def average(numbers):
    total = sum(numbers)
    total = float(total)
    avg = total/len(numbers)
    return avg

def get_average(student):
    homework = average(student["homework"]) *.10
    quizzes = average(student["quizzes"]) *.30
    tests = average(student["tests"]) *.60
    return homework + quizzes + 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"
    return get_letter_grade(get_average(lloyd))


print get_letter_grade(get_average(lloyd))
class_list = [lloyd, alice, tyler]

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


#2

Without even looking at the code this message suggests that the function is ignoring inputs and has a dedicated data structure that will be used in every run of the function. Is your students list inside the function by any chance?


#3

No, the students list is outside of the function.


#4

Then this is the problem. students is the parameter. We need to use it in this loop.


#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]
}

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

def get_average(student):
homework=average(student["homework"])
quiz=average(student["quizzes"])
test=average(student["tests"])
return (.1*homework)+(.3*quiz)+(.6*test)

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=[]
students=[lloyd,alice,tyler]
for student in students:
results.append(get_average(student))
return average(results)

this is my code. I believe ive written a similar code to that written in the question and have used "students" as the parameter. I have also been getting the same error message, what could be the problem? thanks.


#6

This list should not be inside the function, but up in the data section, just after the dictionaries.


#7

Thank you! :slight_smile:


#8

Got it working now. Thanks!


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.