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


#1



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


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]
    else:
        return (n[size/2] + n[(size/2) -1])/2.0


#2

__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


#3

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

Correct code:

n.sort()

or

n = sorted(n)


#4

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