When I run my program on PyScripter, I get the expected median of 4.5
. But running the same code on Ideone as here or on codeacademy returns 4
. Any idea why the different results? Thanks.
#Create a function that returns the median of a list of numbers
def median(list_of_numbers):
#Make a copy of list_of_numbers and sort the new list
lst = list_of_numbers
lst = sorted(lst)
#Get the length of list and use it to index through the list
lst_length = len(lst)
index = int(lst_length/2)
#If length if even, average the two middle numbers
if lst_length%2==0:
a= lst[index-1]
b = lst[index]
result = (a+b)/2
#If length is odd, return the middle number
else:
result = lst[index]
return result
print (median([4, 5, 5, 4]))
My PyScripter version: * Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32. *
For Python 2.x you will need to replace both occurrences of 2
by 2.
in order to enforce floating point division.
Your link to ideone is Python 2.x and apparently so is your codeacademy interpreter.