Error with Median


#1



Median


I'm getting the error "Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5"


I don't understand why this is occurring it seems to me that everything should be fine and I should be getting the correct result.


def median(list):
    list = sorted(list)
    c = len(list)
    if len(list) == 1:
        m = list[0]
        return m
    elif len(list) % 2 == 0:
        a = list[(c / 2) + 1]
        b = list[(c / 2)]
        y = (a + b) / (2.0)
        return y    
    else:
        x = list[len(list) / 2]
        x = x + 1
        x = round(x)
        return x

Thanks in advance for any help


#2

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

so which are the two middle indexes? do you use the correct indexes?


#3

ah thank you i figured it out!


#4