Median: list indices must be integers, not floats


#1



Oops, try again. median([4, 5, 5, 4]) resulted in an error: list indices must be integers, not float


from math import ceil, floor

def median(stuff):
    stuff.sort()

    if len(stuff) %2 == 0:
        print int(stuff[ceil(len(stuff)/2)] + stuff[floor(len(stuff)/2)] /2)

    elif len(stuff) == 1:
        return stuff[0]

    else:
        print int(stuff[ceil(len(stuff)/2)])


#2

you don't need the math module for this from the documentation:
math.ceil(x)
Return the ceiling of x as a float, the smallest integer value greater than or equal to x.

so here:

stuff[ceil(len(stuff)/2)]

ceil will give a float. this needs to be an integer, remove ceil altogether.


#3

Thanks!!!


#4