Median error


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

I can't seem to find the error, this code is working fine in my python console and I get 4.5 but here I get this error, any help why this code is not working?

def median(i):
    if len(i) == 1:
        return i[0]
    elif len(i) % 2 != 0:
        x = int(((len(i)+1)/2)-1)
        return i[x]
        x = (i[int(len(i)/2)-1] + i[int(len(i)/2)])/2
        return x


your console very likely runs python3, look:

print 9 / 2 # outputs 4.5
print 9 / 2 # outputs 4

change where made how division behavior works. To make it work in python2, you will need to do something with float (given dividing an integer by integer floors the number in python2 as you discovered)


Thanks for fast reply, I changed it to float, and it worked!