Just Weight and See - vbeen stuck for ages - get_average(alice) raised the following error: 0


#1


Ex. 6: Just Weight and See


Everything is printed as expected, but for some reason, no matter the changes, it raises the following error:
'Oops, try again. get_average(alice) raised the following error: 0 '


The code seems to be ok, really don't know why it keeps on bringing that up and doesn't let me continue. Would really appreciate it if you could help me with this!


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

students = [lloyd, alice, tyler]

def average(numbers):
    sum = 0
    for number in numbers:
        sum += number
    sum = float(sum)
    avg = sum/(len(numbers))
    return avg

def get_average(student):
    for i in range (0,len(student)):
        homework = average(student[i]['homework'])
        quizzes = average(student[i]['quizzes'])
        tests = average (student[i]['tests'])
        wtotal = [homework, quizzes, tests]
        wavg = 0.1 * wtotal[0] + 0.3 * wtotal[1] + 0.6 *               wtotal[2]
        print student[i]['name']
        print "Homework average = ", homework
        print "Quizzes Average = ", quizzes
        print "Tests Average = ", tests
        #print "Total Average = ", average(total)
        print "Weighted Average = ", wavg
        print "\n"

get_average(students)


#5

This is the issue. We are given one student dictionary. There is no need for a loop since we know the keys in the dictionary, "homework", "quizzes", and "tests".

Only ONE student dictionary is present, so no index.

homework = average(student['homework']) * 0.1
quizzes = average(student['quizzes']) * 0.3
tests = average(student['tests']) * 0.5
return homework + quizzes + tests

We are not asked to print anything to the screen so all the extra time spent trying to do that task is wasted. ONLY do what is asked, and do not embellish or improvise, at least not until you PASS the lesson. After passing we are free to tinker with the lesson all we wish.


#6

Another example of excessive code where a simple solution is possible (and recommended).

We are free to use the built in sum() function. The data contains floats so the internal math will be float arithmetic.

return sum(numbers) / len(numbers)

The above implicitly returns a float. However, if we wish to be explicit, then...

return float(sum(numbers)) / len(numbers)

We did not have any need for additional variables or steps.


#7

Thanks again!

However I'm wondering why does it always raise an error for 'alice' and only 'alice' (not tyler nor lloyd, even if I change the names sequence)?

I get an alice-error on the next exercise, even if I do it in the simplest way.


#8

Codecademy performs various tests on your code, including tests of individual functions. As soon as it finds a problem, it reports back to you.

In testing your get_average function, it first tries it out on alice. It detects the problem and then immediately reports it. That does not mean that the problem would not also occur with lloyd or tyler.


#9

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