# MEDIAN, what's wrong with my code here

#1

``````def median(numbers):
sorted(numbers)
for number in numbers:
if (len(numbers)) % 2 != 0:
position = ((len(numbers) + 1) // 2 )
median = numbers[position]
else:
position1 = ((len(numbers))//2)
position2 = ((len(numbers))//2 + 1)
median = (numbers[position1] + numbers[position2])//2
return median``````

#2

The problem is your algorithm. Look here or here

#3

I have done the same way. May, be I cant see my mistake. could you please figure out the mistake . that would be very helpful. thank you..

#4

``````# a = [1, 2, 3, 4, 5]
# b = [1, 2, 3, 4, 5, 6]
def median(numbers):
numbers = sorted(numbers)
if len(numbers) % 2 != 0: #case a
position = len(numbers)//2 #returns 2
return numbers[position] #returns a[2] = 3
else: #case b
position1 = len(numbers)//2 #retuns 3
position2 = ( len(numbers)//2 ) - 1 #returns 2
return (numbers[position1]+numbers[position2])/2.0 #returns (b[2] + b[3])/2 = (3+4)/2 = 3,5``````

#5

Thank you very much. I am such a fool , couldn't notice the error.

still, there has to float before the statement so that it returns the answer in decimal.
Thank you very much.

#6

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