15.Median


#1



Hey guys, could you help me with this code? It just says median([1]) resulted in an error: list indices must be integers, not float but I do not really understand where to change something...


def median (x): 
    new_x = sorted(x)
    length = len(x)
    if length % 2 == 0:
        return new_x[length / 2.0] - new_x[(length/2.0) - 1] 
    else:
        return new_x[(length - 1) / 2.0]
    print new_x (3,4,5,2,6,8,7)


#2

Indices should be integer, not float.

You're close, but need to re-think this.

Anything that follows return is unreachable. new_x does not exist outside of the function. Print the return value of your function, in global scope.


#3

Ah okay, I got it.

Thanks a lot!:wink:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.