Median lesson problem


#1



def median(lst):
    sorted(lst)
    result=0
    m = len(lst)
    for i in lst:
        if m == 1:
            result += m
        elif (m % 2) == 0:
            median = float(((m/2) + (m/2)-1)/2)
            result += median
        elif (m % 2) == 1:
            median = m/2
            result += median
    return result    
    
print median([4,5,5,4])

Hello every body,

I'm stuck on this one, it gives me the error:Oops, try again. median([4, 5, 5, 4]) returned 4.0 instead of 4.5
Can sameone please help me, I can't get it done.

Thank you in advance!


#2

you sort your list, but you don't store the sorted list anywhere?

you first do the math:

(4 + 5)/2

this will give 4 (dividing two integers will give a integer as result) then you cast the integer to a float


#3

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