8/9 Part of the Whole (get_class_average([alice]) should return a number)


#1

Error = get_class_average([alice]) should return a number

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'])*0.1
    quizzes = average(student['quizzes'])*0.3
    tests = average (student['tests'])*0.6
    return float(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" 

print get_letter_grade(get_average(lloyd))


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

Error = get_class_average([alice]) should return a number


#2

Missing the students list:

students = [lloyd,alice,tyler]

#3

That was typed out in other exercises, but I'm not sure why it got deleted by the time I got to this stage. But regardless it runs through the students - it just doesnt seem to work for alice for some reason. Also, i tried putting that list in, and it makes no difference


#4

Let's take a look at the function:

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

Two returns is a flag raiser, but then so too is the list. This function should return a number, not a list.

def get_class_average(pupils):
    avg = 0
    for student in pupils:
        avg += get_average(student)
    return avg

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

#5

Oops, try again. get_class_average([alice, lloyd]) returned 171.7 instead of 85.85 as expected

That returns the above error.

I'm not sure why mine isnt working, maybe its a glitch? Ive compared mine with couple other codes on here for the same exercise - and I found them to be the same but theres works and mine doesn't. But just in case, these are the exact instructions.

Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.
First, make an empty list called results.
For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
Finally, return the result of calling average() with results.


#6

My bad. Forgot the last step:

def get_class_average(pupils):
    avg = 0
    for student in pupils:
        avg += get_average(student)
    return avg / len(pupils)

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

#7

okay! That worked. Thanks!


#8

Thanks a ton! I'm really sick of the vague, unhelpful error messages Codecademy seems to revel in.


#9

Hi Mtf,

Many thanks your code. It works.

However, I am thinking that could we apply the function average which we have already defined in the previous step. I think the idea of the instruction of creating a empty list RESULTS is that we can use the AVERAGE function which we defined to calculate.

I tried to apply that way a thousand times but something wrong....:frowning:

Much appreciated if you could give the answer as the instruction.

Thanks again.

Celine


#10

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

#11

For anybody who likes to jump ahead, the concept of List Comprehensions comes up in the discussion of Data Structures. Briefly, a list comprehension is list with a self defined internal function that is mapped to itself.

results = [get_average(x) for x in students]

In our project, the results array will look like this:

[ 80.55, 91.15, 79.9 ]

not unlike it does in the earlier example. The return statement would still be the same.

Don't let this peek ahead interrupt your studies in this track. When it comes up, you will be able to look back on this example.


#12

hi i did exactly this and this wont work