Stuck with "Part of the Whole" Exercise


#1



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


I'm trying to complete the exercise "Part of the Whole", but the error message "get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected" keeps popping up. I've compared my code to the correct ones already on the forum, and I can't find anything wrong with my code. Please help :frowning:


Replace this line with your 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]
}

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    return total / len(numbers)
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return homework * 0.1 + quizzes * 0.3 + tests * 0.6

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:
        get_average(student)
        results.append(get_average(student))
        return average(results)

This is my first post here, so please excuse me if I'm making a mistake :slight_smile:


#2

your post is perfect: url of the exercise, allowing us quick access to the exercise so we can run it (makes it easier to bebug), error message and code + markup

the problem is in this line:

return average(results)

You have to realize that a return keyword returns something (hands you something back) causing the function to end. Currently, the return keyword is reached in the first run of the loop, which means the for loop can't append all students to result.

You might want to change the indent of return average(results)


#3

What does that mean? Please explain it better


#4

It means that in the case of ksister code the for loop ends in the first iteration because a return keyword is reached. This is not the desired behaviour, you want to append all students to results not just first one, which means you need to change the indent of return so the whole loop can run

But maybe your problem is different altogether? I recommend you to make your own topic


#5

Thank you so much for your reply! You were right: I changed the indentation, and the code is working now :slight_smile: Thank you so much! :smiley:

I'm not sure if I should post this on a new topic, but I moved on to the next exercise, added some coding, and submitted it. Codecademy says that I've successfully completed the course, but on the actual screen, it says "Traceback (most recent call last): File "python", line 54, in module NameError: name 'results' is not defined." So I'm not sure if my code is correct or incorrect? Could you please check it for me?

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

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)
    return total / len(numbers)
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return homework * 0.1 + quizzes * 0.3 + tests * 0.6

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:
        results.append(get_average(student))
    return average(results)

students = [lloyd, alice, tyler]
print get_class_average(students)
print get_letter_grade(average(results))

Thanks once again!


#6

well, you haven't called the get_class_average yet, so results is still undefined when you try to pass it here:

print get_letter_grade(average(results))

this function call is not a match with the instructions:
Then, print the result of get_letter_grade for the class's average.

the class average is calculated with get_class_average, not average. and you want to calculate for all students so pass students as argument, not results


#7

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