# 15. Median

#1

I can't see what is wrong with my code here...

``````def median(intlist):
sorted(intlist)
if len(intlist) == 1:
return intlist[0]
elif len(intlist) % 2 == 0:
a = len(intlist) / 2
b = a + 1
c = (intlist[b] + intlist[a]) / 2
return c
else:
a = (len(intlist) / 2) - 0.5
return intlist[a]``````

#2

multiply problems. you don't store you sorted list anywhere.

if i have `[4,5,5,4]`, the length is `4`. the indexes: 0, 1, 2, and 3

so what are the two middle indexes? do `a` and `b` contain the right indexes?

Another problem, dividing an integer by an integer results in an integer. you need to do something with float

#3

I tried that but it didnt work. Here is my new code...

``````def median(intlist):
intlist = sorted(intlist)
if len(intlist) == 1:
return intlist[0]
elif len(intlist) % 2 == 0:
a = len(intlist) / 2.0
b = a + 1
c = (intlist[b] + intlist[a]) / 2.0
return c
else:
a = (len(intlist) / 2.0) - 0.5
return intlist[a]``````

It threw the following error...

Oops, try again. median([4, 5, 5, 4]) resulted in an error: list indices must be integers, not float

#4

here:

``a = len(intlist) / 2.0``

list indexes should be integers, so then you should use `2`

#5

I tried this code but still gives an error...

def median(intlist):
intlist = sorted(intlist)
if len(intlist) == 1:
return intlist[0]
elif len(intlist) % 2 == 0:
a = len(intlist) / 2
b = a + 1
c = (intlist[int(b)] + intlist[int(a)]) / 2
return c
else:
a = (len(intlist) / 2) - 0.5
return intlist[int(a)]

#6

you have to decide where to divide by integer and when to decide by float. Now are you just trying. If you need more help, post updated version of your code with markup:

#9

Thinking simple helps.

`a = len(sortedlist) / 2 b = a - 1`