Median


#1



15. median in practice makes perfect

"Oops, try again. median([1]) resulted in an error: list index out of range" it says, i couldnt work it out


can anyone help me please? thanks in advance.


def median(lst):
    numbers =sorted(lst)
    while len(numbers) > 1:
        if len(numbers) % 2 == 0:
            mdn = (numbers[len(numbers)/2] + numbers[(len(numbers)/2)-1])/2
        else:
            mdn = (numbers[len(numbers)/2])
    mdn = numbers[0]
    return mdn


#2

Food for thought:

  • what are you looping over and why?
  • what is your existing program going to return?

hope this helps. I can walk you through your problem if it doesn't, but you'll learn more if you find it yourself


#3

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