Part of the whole


#1



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


It is returning that the average is incorrect. It returns 91.15 as the average instead of 85.85 as expected. "Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected


I cannot for the life of me figure out what is causing it to return an incorrect average.


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"])
    total = (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests)
    return total
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"
    elif score <= 60:
        return "F"
def get_class_average(students):
    results = []
    for student in students:
        a = get_average(student)
        results.append(a)
        return average(results)


#2

A wise man once said; "A function ends the moment a return key is reached."


#3

The wise man also says: "...once it's unindented correctly."


#4

That's exactly what I was saying. Have you ever even read a post by @stetim94?


#5

Yes, I have. He talks about it a lot.


#6

When he talks about that, he is saying that the function will end in the first iteration because you told it to return.


#7

Yes, I know, but where the return keyword is right now, it's going to return the wrong number.


#8

That's what that means.

Of course it's going to return the wrong number; The function breaks after the first iteration of its loop.


#9

Yes, I was just hinting that it needs to be unindented. :slight_smile: That's all.


#10

I understand what you were doing, but I wanted them to figure that out. I would have told them this if they hadn't understood my original statement.

P.S. Don't you mean unindented? :wink:


#11

yes, yes, I've just never heard of that word before (or it was never mentioned to me EVER.:thinking:) But there is always a 'un' to any word. Like 'un'indented :stuck_out_tongue:
you learn something new every day.:stuck_out_tongue_winking_eye:


#14

2 posts were split to a new topic: Get_class_average([alice]) resulted in an error:


#16

A post was split to a new topic: Global name 'studetns' is not defined


#18