Median: Index error


#1



Median


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


I can't understand the error.


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


#2

What would you say the median is for that list?
If you claim it's 5, then go read up on what median is, wikipedia would be a good place to start
If you agree that it's 4.5, then what steps did you carry out to end up at that value? Are those steps present in your code?


#4

thanks!
i forgot to sort first .:no_mouth:


#5

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