# 15.Median - Cant find my mistake

#1

So heres my code for the median function:

def median (List):
--lent = len(List)
--List = sorted(List)
--if lent%2 == 0:
----return ( ( List[(lent/2)-1] + List[(lent/2)] ) / 2 ) #since index start counting from 0, I substract 1 to the len/2 eg: in [1,2,3,4] I would get (4/2)-1 to get index[1], so I add numbers 2 and 3, and then divide by two to get the average.
--else:
----return ( List[(lent-1)/2] ) #since list has odd amount of numbers y substract 1 off the lenght and divide to locate the middle number

when I run, console says: " median([4, 5, 5, 4]) returned 4 instead of 4.5"
i really cant find any mistake in my code, I commented the whys of each operation. Any help would be appreciated.

#2

`List` is a Python built-in class name, and not a suitable variable name. Convention also provides that we only use a capital first letter to denote a class, not a function or variable. We can of course call the variable anything we like, avoiding classes and reserved words. Descriptive is good, but in a simple situation like this we can even use `x`.

``def median(x):``

The main cause of the error is that you are not returning a float when averaging the middle two terms. Divide by 2.0.

#3

Thanks a lot, it worked. Now it all makes sense, I was trying to get a float out of an integer operation.
Btw, you are also right about the variable name, it can certainly cause confussion. Thanks again.