15. Median


#1



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


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


def median(list_name1):
list_name=sorted(list_name1)
size=len(list_name)
print(list_name)
if size%2==0:
a=list_name[int(size/2)]
b=list_name[int((size/2)-1)]
return (a+b)/2.0
else:
z = int((size/2)-0.5)
return list_name[z]
I tried this in anaconda(spyder terminal) and it returned 8, but the error said that it returned 6.
Why it isnĀ“t working in codecademy?


Replace this line with your code.


#2

Not necessary to fudge the data with a float. On an odd length list, int(size / 2) will be the middle index.


#3

Thank you, I dind't know that, now my code works fine. :grinning:


#7