What's the problem?


#1

Here is my code:

def median(alist):
    something = sorted(alist)
    for i in something:
        if len(something)%2 == 0:
            return (something[len(something)/2]+something[len(something)/2-1])/2
        elif len(something)%2 != 0:
            return something[len(something)/2]

#2

I don't think the for loop adds anything.

Also I think if alist contains only integer values, your function only returns integer values, even though the median could be a float.


#3

as @eveat has pointed out the for loop in your code does not do anything so remove it. Also you just need to modify this line

return (something[len(something)/2]+something[len(something)/2-1])/2

to this

return (something[len(something)/2]+something[len(something)/2-1])/2.0

this is so the result return is a float(and not an int) so we donot loose data


#4

Thanks it worked and you were right, I didn't need the for loop.