Median error: list indices must be integers not float


#1


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


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

I do not understand what i'm doing wrong.

The hint states that we should divide by 2.0 to achieve a float value.

please help!


def median(myList):
    m = sorted(myList)
    listLength = len(m) 
    if listLength % 2 == 0:
        i = listLength/2.0
        med = (m[i] + m[i-1])/2.0
        return med
    else:
        i = (listLength-1)/2.0
        med = m[i]
        return med


#2

Just figured out what i was doing wrong.

the value of the indice 'i' was to be calculated as an integer and the median 'med' as a float.
Using i =listLength/2 solved it.


#3