Median


#1

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

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

Why the code is not working?

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

#2

This will not assign anything to lst, since List.sort() is an in place method. Use either,

lst.sort()

or

lst = sorted(lst)

#3

sort() will update the list for you, don't store the result of sorting in a variable


#4

Yeah it worked :smile: thank you!


#5

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