Median : object of type 'NoneType' has no len()


I get an error saying "median([1]) resulted in an error: object of type 'NoneType' has no len()". I have tried to google and search, but did not become any wiser. I suspect that it has to do with numbers being counted as empty after numbers=numbers.sort(). I did also try to create a separate list with sorted(numbers), but I got the same error.

def median(numbers):
    if len(numbers) == 1:
    elif (len(numbers) % 2) == 1:
    elif (len(numbers) % 2) == 0:
    print median_value
    return median_value


sort() will update the list for you, no need to store the result in a variable

actually, sort doesn't return anything, so the default (None) is returned. And None(Type) doesn't have a length.


You will get one more error after solving the None error as @stetim94 mentioned.

In this line of code...

There is difference between these two---

float(5 / 2)
float(5) / 3


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