Please help me find the errors in my code!


#1

Hi guys,
My code does not seem to be working for some reason

The code returns 4 instead of 4.5 when looking at (4,5,5,4)

I am not sure why it does not return 4.5, please help me out if possible!

def median(lst):
    
    lst.sort()
    length = len(lst) 
    med = (len(lst) + 1)/2
    medi = 2*med 
    thingy = (lst[length/2] + lst[(length/2) -1])
    thingy2 = thingy/2
    if med%2 != 0:
        return lst[med-1]
    elif len(lst) == 1:
        return i 
    else:
        return (lst[length/2] + lst[(length/2) -1])/2

#2

here:

thingy2 = thingy/2

for [4,5,5,4] thingy is nine:

thingy2 = 9/2

problem in python2, dividing two integers returns an integer (if needs be, the number is floored), try dividing by 2.0, this way you get a float


#3

Thank you very much!


#4