Median: List isn't sorting correctly


#1


Error returns:
"Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5"


I'm sure there's loads more wrong with this code, but I can't figure out why the list isn't being sorted.


def median(x):
    x.sort()
    middle = 0
    count = len(x)
    middle = count / 2
    if count == 1:
        median = x[0]
    elif count > 1 and count %2 == 0:
        add = x[middle] + x[middle +1]
        median = add / 2.0
    else:
        median = x[0]
    return median


#2

your list is after sorting: [4,4,5,5], agree? the length of the list is 4. So middle (4/2) = 2.

but the items are indexed at 0,1,2,3. So want middle -1, not +1


#3

Thank you so much. With a few other tweaks, my code now runs successfully!