List indices must be integers


#1



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


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


It's ugly, but I don't quite understand what's going wrong with it.


def median(numbers):
    sorted_list = sorted(numbers)
    node = len(sorted_list) - 1
    half = len(sorted_list) / 2
    from_bottom = 0 + half
    from_top = node - half
    middle_together = from_bottom + from_top
    if len(sorted_list) % 2 == 0:
        middle = sorted_listmiddle_together / 2
    else:
        middle = sorted_list[node - node * .5]
    return middle


#2

Make sure you don't have any decimals in the numbers.


#3

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