# Median help pls

#1

Oops, try again. median([4, 5, 5, 4]) returned 5 instead of 4.5

my sortedlist isnt [4,4,5,5]. why is that? how can I fix it? thx for ur reply

``````def median(mylist):
sortedlist = []
while mylist != []:
transfer = min(mylist)
sortedlist.append(transfer)
mylist.remove(transfer)
length= len(sortedlist)
if length == 1:
return sortedlist[0]
elif length % 2 == 1:
odd = ((length-1)/2)+1
return sortedlist[odd]
else:
a = sortedlist[length/2]
b = sortedlist[length/2+1]
return (a + b)/2``````

#2

`` [4,4,5,5].``

the items are indexed at: 0,1,2,3 and the length is 4, so this:

``a = sortedlist[length/2]``

will get you the item at index 2, where as this:

``b = sortedlist[length/2+1]``

will get you the item at index 3. Uhm.. the middle two items are at index 1 and 2, not 2 and 3?

#3

Thx!

By my understanding

``````    a = sortedlist[(length/2)-1]
b = sortedlist[length/2]``````

should solve the problem, because this code would give me the first and second index places, for the example. Yet, this doesn't work either.

#4

let me guess, the error message you are now receiving is:

Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5

diving two integers gives a integer in return in python2, use `float()` or divide by a float (2.0 for example) to overcome this problem

#5

Wow, yes, I forgot that! Thanks for your quick feedback!

#6