15/15 Traceback error


My code:
(tab issues)

def median(my_list):
   my_list = sorted(my_list)
    if len(my_list) % 2 == 1:
        for i in range(len(my_list)//2):
            my_list = my_list.remove(my_list[0])
            my_list = my_list.pop()
            print my_list[0]

Traceback (most recent call last):
File "python", line 9, in
File "python", line 6, in median
AttributeError: 'NoneType' object has no attribute 'pop'


So the problem here is that Python is finding that you're trying to call pop() on an object which does not have that method/function defined.

This usually indicates an error in assignment somewhere. Do you have the full code, or is this it?


@ajaxwhiz08742 here is the issue list.remove method is a void method() which means that it does not return any value back to its caller(So in a sense gives None)
Now you wrote

my_list = my_list.remove(my_list[0])

Try to insert a print statement like this

print my_list #None

after it and see what you get.
Remember my_list is the supposed to be the the sorted form of the input list originally passed right.
But in the line you wrote above you re-assigned it to be a None Hence in the first run of the for loop

my_list = None

The interpreter then moves to the next line which is

my_list = my_list.pop()

but you have changed the value of my_list to None and the list.pop() method can only be use on objects of type list and nothing else hence you will get an
AttributeError: 'NoneType' object has no attribute 'pop'

Also you do not need any loops for this exercise