Median - can't figure out what's wrong


#1



ok im getting a little bit pissed right now. i looked all over the forum and still couldn't figure out what is wrong. can someone please help me, can someone please explain to me step by step why my code does't work



Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5


i don't understand why i don't get 4.5 instead of 5.0. i tried with the float() but still, 5.0


def median(some_list):
    lenght = len(some_list)
    for i in sorted(some_list):
        if lenght == 1:
            return some_list[0]
        if lenght % 2 == 0:
            one = some_list[lenght / 2]
            two = some_list[(lenght / 2) - 1]
            res = one + two
            return res / 2.0
        elif lenght % 2 != 0:
            some_list = some_list[:-1]
            one = some_list[lenght / 2]
            two = some_list[(lenght / 2) - 1]
            res = one + two
            return res / 2.0


#2

from the information of the exercise:

The median is the middle number in a sorted sequence of numbers

imported word here: sorted. You don't sort your list


#3

for i in sorted(some_list)
isn't that sorted list?


#4

what do you need a for loop for? You don't need it

The for loop now loops over a sorted list, but here:

one = some_list[lenght / 2]

some_list isn't sorted, that is only for the loop.


#5

thank you very much!!!!


#6