15.Median Help


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


It keeps sending me 'Oops, try again. median([1]) resulted in an error: object of type 'NoneType' has no len()'. Why?


def median(lis):
    new = lis.sort()
    if len(new) == 1:
        return new[0]
    else:
        if len(new) % 2 == 0:
            return (new[len(new)/2.0] + new[len(new)/2.0 - 1]) / 2.0
        else:
            return new[(len(new)-1)/2.0]


#2

Never mind, I got it.


#3

This works :slight_smile: tell me an efficient method if any

def median(lis):
    lis.sort()
    if(len(lis)%2==0):
        return (lis[len(lis)/2]+lis[(len(lis)/2)-1])/2.0
    else:
        return lis[len(lis)/2]
print median([2,6,3,8,5])

#4

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