Code not working properly


#1



why does this code doesn't work?


def median(listx):
    count=len(listx)
    listk = sorted(listx)
    if count%2 == 0:
        m1= listk[((count-1)-1)/2]
        m2 = listk[((count-1)+1)/2]
        mediane= (m1+m2)/2
    else:
        m3=listk[(count-1)/2]
        mediane = m3
    return mediane
        
listy=[1,4,2,6,8,3,4,2]
print median(listy)


#2

the problem is here:

mediane= (m1+m2)/2

because the division only involves integers, the result will be an integer as well (if needs be, python will floor/round down to the nearest integer)

Pretty big hint, see if you can figure it out. Otherwise there is another hint here:


the division should involve a float to prevent the number from getting rounded down

#3

thanks mate! :smiley:


#4