Median 15: error message doesn't make sense


I'm getting the following error message:
Oops, try again. median([6, 8, 12, 2, 23]) returned 12 instead of 8

The code:

def median(numbers):
    sort = sorted(numbers)
    l = len(sort)
    if l % 2 ==0:
        index1 = l/2
        num1 = numbers[index1]
        num2 = num1 -1
        return (num1 + num2)/2.0
        return numbers[l/2]


Except 8 is the median. To find the median you first have to organize the list so that it is in numeric order and then you take the middle number.


Oops thanks!
I forgot to put sort at the bottom...


Remember that you have sort = sorted(numbers) So in the function change numbers to sort that way everything is being ran against the sorted list of numbers.


Got it! Forgot that the second number is -1 on the index, not the value.


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