Median


#1



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


Oops, try again. median([1]) resulted in an error: 0 is not in list


what? i have a special function for 1 lists


def median(lst):
    lst = sorted([lst])
    length = len(lst)
    if length == 1:
        return lst.index(0)
    elif length % 2 == 1:
        length /= 2 
        item = lst.index(length)
        return item
    else:
        length /= 2
        obj1 = lst.index(length)
        obj2 = lst.index(length - 1)
        item = obj1 + obj2 / 2 
        return item


#2

This has the unfortunate effect of making the list have only one element.


#3

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