Median - Oops, try again. median([6, 8, 12, 2, 23]) returned 12 instead of 8


#1



Hi, below is result of the program

('DEBUG ordered list is: ', [2, 6, 8, 12, 23])

('MEDIAN result is: ', 8)

None

but the following error message occurs.

Oops, try again. median([6, 8, 12, 2, 23]) returned 12 instead of 8

Any idea why there is an error message when the median result is correct?


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


Oops, try again. median([6, 8, 12, 2, 23]) returned 12 instead of 8


Replace this line with your code. 
def median(sequence):
    if len(sequence)% 2 == 0 : # the number of values is even
        print ("DEBUG",sequence[int(len(sequence)/2)])
        print ("DEBUG",sequence[int(len(sequence)/2)]-1)
        median =(sequence[int(len(sequence)/2)] + sequence[int(len(sequence)/2)]-1)/2.0
        return median
    else:  # the number of values is odd
        median = sequence[int(len(sequence)/2)]
        return median


nbrList = [6,8,12,2,23]
nbrList.sort()
print("DEBUG ordered list is: ", nbrList) 
print(median(nbrList))


#2

You didn't sort() your list.


#3

@ri_chiu, the exercise will call the function multiply times for validation purpose, so you should sort the list inside the function


#4

Hi,
Many thx for your prompt answer!
I 've changed the code (moving the sort built-in function inside the median function) and one minor correction.
Now, it works great!
Thanks for your support.


#5