# Practice Makes Perfect - Median - solution working in IDLE but not codecademy

#1

I'm trying to create the median function in Practice Makes Perfect, I'm getting a response saying that for a list of numbers [4,5,5,4] it returns with 4 instead of 4.5. But in IDLE my solution seems to work fine. An F5 reset of the browser isn't fixing it. Anybody able to assist?

As way of explanation, I'm storing the index position as a variable and then using that to determine the two middle numbers of an even number of list items.

def median(numList):

list_length = len(numList)

new_list = sorted(numList)

final_median = 0

if list_length % 2 == 0:

index_first_middle_no = int(list_length / 2)
index_second_middle_no = index_first_middle_no + 1

middle_no_1 = new_list[index_first_middle_no - 1]
middle_no_2 = new_list[index_second_middle_no - 1]

final_median = (middle_no_1 + middle_no_2) / 2

else:

index_middle_number = int((list_length / 2) + 0.5)

final_median = new_list[index_middle_number]

return final_median

#2

the problem is in this line:

final_median = (middle_no_1 + middle_no_2) / 2

the problem is that in python3 behaviour of dividing was changed. Codecademy runs python2. your IDLE runs python3.

in python3, numbers are no longer floored when dividing two integers (good change)

to overcome this problem in python2, divide by 2.0 instead of 2

#3

Perfect! Thank you

#4