Student Becomes the Teacher: My output seems correct, but codeacademy says it's wrong


Hello everyone. I'm trying to get the entire class average, but when I run my code, codeacademy says "Oops, try again. get_class_average([alice]) returned 85.69 instead of 91.15 as expected."
But the output in my console window seems to be returning the correct value of 91.15 for alice.

Here is my 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]

def average(numbers):
    total = float(sum(numbers))
    a = total/len(numbers)
    #print "total for average: %g" % (a)
    return a
def get_average(student):
    homework = round(float(average(student["homework"]) *0.1), 2)
    quizzes = round(float(average(student["quizzes"]) *0.3), 2)
    test= round(float(average(student["tests"]) *0.6), 2)
    final = homework + quizzes + test 
    print "final for student average is %g" % (final)
    return final
studs = [lloyd, alice, tyler]
results = []

def get_class_average(students):
    for student in students:
        result = float(get_average(student))
    print results
    print round(average(results),2)
    screw_you_compiler = round(average(results),2) 
    return screw_you_compiler
print get_class_average(studs)


I find that looking for ways to reduce the code to its simplest form is the best way to ferret out errors in syntax and logic. Take for example,

We can reduce this to,

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

While variables kind of make things easier to understand, I'm more partial to working with the fewest possible. In this case, just the argument itself.

goes a little overboard, I would say. We should not be rounding as that will affect calculations further down the food chain, if this is only a secondary step. Keep the floats as they are. More to the point, average() returns a float so we need not declare that on any of these computations. They are floats, still.

def get_average(student):
    homework = average(student["homework"]) * 0.1
    quizzes = average(student["quizzes"]) * 0.3
    tests = average(student["tests"]) * 0.6
    return homework + quizzes + tests

This is a computational function and we should not be doing any printing.

In the instructions we are asked to name this list, students. To keep your code organized, I would recommend writing it above the code, just under the three dictionaries (what could be called the data section).

Again, too much going on here. As another computational function, there should be no output, only a return value. We need to initialize result locally else it carries old values into the function each time it is called. Setting inside ensures it is empty at the start of the loop.

def get_class_average(students):
    result = []
    for student in students:
    return average(result)

Try to pass without printing anything. Once you do, you can add,

print get_class_average(students)

or any other print statements you wish to use to test your code. Remember to keep it simple and as close to the instructions given as possible. Once you pass, you are free to tinker all you wish. Pay close attention to any console errors. Those are ones we need to learn how to interpret and mitigate.


My code was originally much simpler. I added stuff on to try and figure out what was causing the problem. Although it looks like taking out the debugging print statements and putting result = [] inside the function did the trick! It's happier with a single output. Thanks!


I have the same issue. The numbers add up and are returned, yet code-academy says they are wrong. Does anyone have what the expected output looks like.


While above I did suggest we could do all the printing outside of the functions, I went against my word and tried the following, which gave a pass.

def get_class_average(pupils):
    sum = 0
    for pupil in pupils:
        avg = get_average(pupil)
        glg = get_letter_grade(avg)
        print "%s: %.2f => %s" % (pupil['name'], avg, glg)
        sum += avg
    return sum / len(pupils)
class_bar = get_class_average(students)
print class_bar
print get_letter_grade(class_bar)


Lloyd: 80.55 => B
Alice: 91.15 => A
Tyler: 79.90 => C

If your code still results in an error, then it will be one of the functions that is broken.


Which part are you on exactly? For section "Part of the Whole", it should return:

None "

And ONLY that. Part of my issue is that I had other things printing so I could double check everything was working properly. For example, I had the average of each student printing, as well as the class average, and it didn't like that I was printing excess things.


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