Weight (and see) Issues


Hi there, I seem to be somewhat stuck on the excercise 'Weight and see'


My code is as follows:

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

When i try to run it, I get the following error:
Oops, try again. get_average(alice) raised the following error: unsupported operand type(s) for /: 'list' and 'int'

However, if I edit out the get_average function and try to run the following line

print alice['homework']

This will work just fine!

I thought that this would be exactly how it is called within the function, but I guess this isn't the case then.. So anyway, hopefully someone here can explain where I went wrong!


but you need to get the average homework grade, this means the sum of list divided by the length, here:

student['homework'] / len(student['homework'])

student['homework'] is not the sum of list, its the list itself, you can't divide list by integer (length of list), that will throw an error as you can see

that is why we wrote the average() function, we can use it here


In get_average(student) function , You are trying to divide list with integer.

This means..
say for tyler..

student['homework'] / len(student['homework']) -- > [0.0, 87.0, 75.0, 22.0] / 5

This will cause us an error.

as mentioned by @stetim94, You need to use your helper function average()


Ah, I see. That makes sense.
So I now rewrote the function to look like this:

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

I thought this would do the trick but I now get the error:
get_average(alice) raised the following error: unsupported operand type(s) for *: 'float' and 'NoneType'



while defining the average() function we needed to return the result (total).


Aaahhhh, right. That has indeed done the trick!
*note to self: always end functions with return

Thank you guys for the fast replies!!


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