Not expected return of median([4, 5, 5, 4])


#1




The Function "median(lst)" returns the wrong value for x. It returns 4.0 instead of 4.5


I expected that it would return 4.5, because when I got the Codecademy error message "Oops, try again. median([4, 5, 5, 4]) returned 4.0 instead of 4.5", I tested the script on my computer with Python version 3.5. And it returned me the right value for x(it is 4.5). Why did the Codecademy Python interpreter returned me a different result than the the Python 3.5 interpreter on my computer? Any hints(I said hints, not solutions.)


def median(lst):
    y = lst
    y.sort()
    if len(y) % 2 != 0:
        z = int(len(y) / 2)
        x = y[z]
    else:
        z = int((len(y) / 2) - 1)
        z = int(z)
        x = float((y[z] + y[z+1]) / 2)
    return float(x)


#2

Because the codecademy course is in Python 2 and even though the differences may be bijou there are still significant.


#3

What do you think might be the change(s) between Python 2 and 3 that are responsible for a different function return?

Edit: Thanks ,bandit, I looked for the changes between Python 2 and 3 and discovered a change in the way that Python returns the results of a division. I changed some few things and now it works. Thanks. :grinning:


#4

In remission to Control Flow of the return function it is handled slightly differently in Python 2 than 3:slight_smile:


#5

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