# 'It's ok to be average'- What's wrong?

#1

Hi! So this 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):
numbers = tyler[1:], alice[1:], lloyd[1:]
total = sum(numbers)
total = float(total)
result = total / len(numers)
return result

And when I save it and try to run it it says : Hops, try again. average([3, 0]) resulted in an error: unhashable type,

I dont get it… Can anybody explain/help ?

#2

In this exercise, you are supposed to create a function that will take one argument - list of numbers and will return average of them.

So, you should be able to perform this function call:

``average([1, 2, 3])``

Your function changes the value of passed argument (`numbers`) in the first line to calculate average for students. That's the problem. You should delete the first line and correct one typo (`numbers`, not `numers`):

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

unhashable type

Take a look at `tyler` object. This is not a list. That is why you can't do something like this `tyler[1:]`. Order of properties in objects does not exist.

#3

Sounds right, but when I try to do it I get another messag againwhich says : 'Oops, try again. average([3, 0]) resulted in an error: global name 'numers' is not defined'...

#4

You should delete the first line and correct one typo (`numbers`, not `numers`)

You have to change `numers` in this line:

``result = total / len(numers)``

to `numbers`.

#5

AHHH, Thank you so much, I totally didn't see that!

BUT…

'Oops, try again. average([3, 0]) resulted in an error: global name 'result' is not defined'…

#6

Can you post your code after correction? I would like to take a look at indentation levels so please paste your code, select it and click `</>` icon in the toolbar above the text area.

Or you can use https://gist.github.com/, just paste your code there, click create gist and post here a link

Or just take a screenshot of your code.

#7

I think, you should convert the len(numbers) to a float, using this
result = total / float ( len(numbers))

My code below :
def average (numbers):
total = sum(numbers)
float (total) == total
total = total/ float(len(numbers))

#8

This is not needed, because @ksiezniczkania has converted value of `total` to the float.

#9

I did the same, but it doesn't work, I needed to convert it to float to make it work.

#10

Are you sure? Please open the exercise and try this code:

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

There is no need to convert both operands to `float`.

#11

It does work,

my code is this :

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

I think, I submit each time a float to an integer, the float receive the integer, that's why I needed the float for the len(numbers)

#12

I am sorry, but this line:

``float (total) == total``

does nothing, it does not change value of `total`. And this is why you had to convert `len(numbers)` to float, because `total` is still an integer.

Corrected line:

``total = float(total)``

So, just like in solution proposed by me, only one operand of the division is a float.

#13

def average(n):
total = sum(n) / len(n)
total = float(total)
print (total)

average([3, 0])
1.5

it's wrong ???

Oops, try again.
Make sure you converted the total or the length to a float before
the division - average([3, 0]) returned 1.0 instead of 1.5 as expected.

it's not true!
I'm wasting time here.

#14

Yes, it is wrong. For me your code returns `1.0` -> https://repl.it/CuX1

Let's say that `n = [3, 0]`, just like in the error message. This is how your function works:

``````sum([3, 0]) = 3
len([3, 0]) = 2
total = 3 / 2 = 1
float(total) = 1.0``````

Do you see a problem? After division, it's too late to change the value to float.

You need to change this line:

``total = sum(n) / len(n)``

to:

``total = float(sum(n)) / len(n)``

or to:

``total = sum(n) / float(len(n))``

And now you can delete this line:

``total = float(total)``

Is this clear?

#15

it's clear

total = float(sum(n)) / len(n)
it's ok

thank's!

#16

You're welcome!

#17

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