15.median - help please


#1


I need help with my code please!


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


I keep getting this error -
Oops, try again. median([1]) returned [1] instead of 1


Replace this line with your code. 
def median(stuff):
    sorted(stuff)
    for things in stuff:  
        num = len(stuff)
        if num % 2 != 0:
            while len(stuff) > 1:
                stuff -= min(things)
                stuff -= max(things)
            return stuff
        elif num % 2 == 0:
            while len(stuff) > 2:
                stuff -= min(things)
                stuff -= max(things)
                return stuff
                average = (things[1] + things[0]) / 2.0
            return average


#2

This is not an in-place sort. It needs to be assigned to a variable. To do a self-state change, use the method,

stuff.sort()

otherwise,

temp = sorted(stuff)

The for loop is unnecessary. The while loops would do the job if the for loop wasn't preventing then from working. We cannot mutate the iterable in a for..in loop.

There is a much simpler way to approach the problem than brute force destruction of a list. Not that this way isn't somewhat novel. We had one such procedure pass this way a while back.


#3

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