15. Median - slice it?


#1



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

I get the error-message: "Oops, try again. median([4, 5, 5, 4]) resulted in an error: object of type 'int' has no len()"

Why is it like this? My first idea was to slice it, and now that I failed I did it like the hint suggested. Still, it would be nice to know if there is a way to complete this exercise while slicing! (Even if my code is wrong, please tell me how) Any help is appreciated!


def median(numList):
    numList = sorted(numList)
    
    if len(numList)%2==0:
        while len(numList)>2:
            numList=numList[1:]
            numList=numList[-1]
        result = float((numList[0]+numList[1]/2))
        return result
        
    else:
        while len(numList)>2:
            numList=numList[1:]
            numList=numList[-1]
        return numList[0]


#2

purely looking at the error message you are presented with, i would say these 3 lines give you a problem:

        while len(numList)>2:
            numList=numList[1:]
            numList=numList[-1]

why? In the first run, numList is a list. But then, you overwrite numList, and it becomes a integer, which is what the error message is indicating:
error: object of type 'int' has no len()
len() is a function, to determine the length of a list, but we just determined, that you overwritten your list with a integer, a integer doesn't have a length.

Keep in mind that you can overwrite variable.