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