Median


#1



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


Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


I think I put the right codes for even numbered list to return the average of the middle two indexes. So I can't figure out why the error message keeps occurring.


Replace this line with your code. 
def median(lista):
    lista.sort()
    if len(lista) < 2:
        return lista[0]
    elif len(lista) % 2 == 0:
        even1 = len(lista) / 2
        even2 = even1 - 1
        return (lista[int(even1)] + lista[int(even2)]) / 2
    else:   
        odd = (len(lista) + 1) / 2
        return lista[int(odd)]


#2

point1
In python 2.0
use of /
If you want decimal value you need to make one of the numbers a float.

ex..

4 / 3 -- > 1
4 / 3.0 --> 1.333

point2.

Check you final else part,You're adding one to find mid number(index of mid number)?


#3

NVM I figured out why. I just had to divide the even numbered list by '2.0' not 2 so as to be able to return float, not integer.


#4

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