15. Problems with the topic Median


#1



15/15 Median.


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


I've been stuck here for a long time, could please someone help me to check my code, thanks!


def median(numbers):
    
    r = numbers
    r.sort()

    if len(r) % 2 == 0:
        return (r[len(r) / 2 - 1] + r[len(r) / 2]) / 2
            
    else :
        return r[(len(r) - 1 ) / 2]


#2

From the hint section of the module:

Last but not least, note that (2 + 3) / 2 is not the same as (2 + 3) / 2.0! The former is integer division, meaning Python will try to give you an integer back. You'll want a float, so something like (2 + 3) / 2.0 is the way to go.


#3

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