15. Median


#1



I get the following response: Oops, try again. median([1]) resulted in an error: list index out of range. I don't quite get what it means. I checked everything a couple of time but still can't see what is wrong. Thank you in advance!


def median(numbers):
    median=0
    numbers=sorted(numbers)
    length=len(numbers)
    if length%2==0:
        median=median+(numbers[length/2]+numbers[length/2+1])/2
    else:
        median=median+numbers[(length/2+1)]
    return median


#2

[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]  # int(9 / 2) == 4
             [4]
              5                => median == 5


[ 1, 2, 3, 4, 5, 6, 7, 8 ]
          [3][4]
     float(4 + 5) / 2          => median == 4.5

#3

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