# Need help with the weighted average exercise

#1

Hello,

The following throw codeacademy error.

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

students=[lloyd,alice,tyler]
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)

#2

That return value looks like a tuple. Remove the parentheses.

#3

Thanks. I still get the following error.

Oops, try again. get_average(alice) raised the following error: global name 'average' is not defined

#4

Did you skip a lesson? There should be an `average` function that takes a list and computes an average which it then returns. How might that function be written?

#5

No, I did it in the last session. Do I have to copy that code on top of this?

#6

Yes. It should have carried forward, but if not then add that code in, and also the `students` list (just under the dictionaries).

#7

I did it but I am still getting error. following is the link to my code

#8

That link takes me to my code, not yours. We cannot see each others' lesson work. Please post your completed code (leave out the dictionaries since we can see them above). I didn't notice the students list in your first post. Be sure it is still present.

#9

students=[lloyd,alice,tyler]

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

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

#10

Try this little bit of test code to see what the function generates:

``````for student in students:
print average(student)``````

What values are returned? Remove the code when done.

#11

The following error: Oops, try again. get_average(alice) raised the following error: unsupported operand type(s) for +: 'int' and 'str'

students=[lloyd,alice,tyler]

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

def get_average(student):
for student in students: #your code inserted her
print average(student)
homework = average(student["homework"])
quizzes = average(student["quizzes"] )
tests = average(student["tests"])
return 0.1*homework+0.3*quizzes+0.6*tests

#12

These lines are not needed. Only one student at a time is being evaluated.

The test code I gave you can be inserted at the end of your code and just allowed to run. The output will display in the console.

#13

In the output window:

Traceback (most recent call last):
File "python", line 33, in
File "python", line 22, in average
TypeError: unsupported operand type(s) for +: 'int' and 'str

Oops, try again. There seems to be something wrong with your code, see the console window for the error message!

#14

Many thanks for your effort in trying to help me. I have found the bug. When you indent a code line. The continuation of the code line to the following line need not to be indented. Otherwise the compiler would read spaces.

Best wishes,

#17

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