Median 15: error message doesn't make sense


#1




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
    else:
        return numbers[l/2]


#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.


#3

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


#4

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.


#6

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


#7

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