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

#1

Hi, below is result of the program

('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?

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

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