Median error


#1



https://www.codecademy.com/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 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):
    i.sort()
    print(i)
    if len(i) == 1:
        return i[0]
    elif len(i) % 2 != 0:
        x = int(((len(i)+1)/2)-1)
        return i[x]
    else:
        x = (i[int(len(i)/2)-1] + i[int(len(i)/2)])/2
        return x


#2

your console very likely runs python3, look:

#python3
print 9 / 2 # outputs 4.5
#python2
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)


#5

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


#9