Median - error: 'NoneType' object has no attribute '_getitem_'


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

def median(n):
    size = len(n)
    n = n.sort()
    if size%2 == 1:
        return n[size/2]
        return (n[size/2] + n[(size/2) -1])/2.0


__getitem__ refers to the []-operator, and None is a value representing lack of value, which is what you last assigned to the varible you use [] with


The sort() method does not return anything, it only changes the original list.

Correct code:



n = sorted(n)


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