Problems with the median exercise


#1


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

When I execute my code, it gives me an error:
Oops, try again. median([4, 5, 5, 4]) resulted in an error: object of type 'NoneType' has no len()

I expected the code to work as intended. Is there a way to convert the input into a type list?

def median(numbers):
    if len(list(numbers)) == 1:
        return numbers[0]
    nums_list = list(numbers).sort()
    if len(nums_list) % 2 == 0:
        index = len(nums_list) / 2
        total = (nums_list[index] + nums_index[index + 1]) / 2.0
    else:
        index = int(nums_list / 2) + 1
        total = nums_list[index]
    return total

#2

Look at all the places where you check the length, one of those values is None (representing lack of value, nothingness) can you spot which one? It's quite alright if you can't, but then you'll need to find out. Add print statements to your function which print out the values that your are getting the length of - at least one of them will have the value None and you'll have to look at what you last assigned to that variable.

The prints aren't really required though, because the error message from Python will already let you know where it happened. So test your function with the input that is mentioned in the error message from Codecademy and then look closer at what error is printed out by Python.


#3

I believe numbers (the parameter of the function) is already a list when it is passed. So to reference the list, you don't need to try anything like list(numbers), instead you can just use numbers itself.


#4

Correct, that's a redundant operation. It's just not the source of this error message
There's also no need to check for if there's one element, that's already covered by the case for odd amount of elements.


#7

Wow. such a passive aggressive ■■■■ ionatan. The source of the problem is that unlike most languages, .sort() is a modifier and does not return a new list, so he doesn't need a reassignment when he sorts it. Just ■■■■■■■ say that instead of being a pretentious ■■■■.