Median- code works in pycharm but not in codecademy?


#1

For problem "median" in practice makes perfect

When I call the below function in pycharm it returns 4.5.
When I call it in codecademy it returns 4.
Anyone know why?


def median(x):
    lst=sorted(x)
    if len(x)%2!=0:
        a=int((len(x)/2))
        c=lst[a]
    else:
        a=int((len(x)/2)-1)
        b=int(len(x)/2)
        c=(lst[a]+lst[b])/2
    return c

median([4,5,5,4])


#2

It works in Python 3.x because a float is returned. This exercise is interpreted with Python 2.x so an integer is returned.

c = float(lst[a] + lst[b]) / 2

#3

Ah thank you so much!


#4

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