# #15 why doesnt the code work...?

#1

Oops, try again. median([1]) resulted in an error: list index out of range

def median(numbers):
numbers.sort()
if len(numbers)%2!=0:
middle=numbers[len(numbers)/2]
elif len(numbers)%2==0:
middle=(numbers[len(numbers)/2]+(numbers[len(numbers)/2]-1))/2.0
return middle

#2

now the error is -->Oops, try again. median([1, 34, 1, 6, 8, 0]) returned 5.5 instead of 3.5
the list doesnt sort, but I have written the number.sort() code.

#3

Why would it work?
What steps is it supposed to carry out?
Is it doing those steps? How can you find out?

If you've identified that it doesn't sort (don't guess, find out) then how can you look up how sorting is done in python?

#4

oh my god , I fixed it , I was writing the step by step, what would my code do for the error message's code and noticed, that (numbers[len(numbers)/2]-1) --> the "-1" is outside the "]" so I fixed it>> middle=(numbers[len(numbers)/2]+(numbers[len(numbers)/2-1]))/2.0, Thank you so much

#5

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