Help! Median


#1

This is my code:
def median(numbers):
new=sorted(numbers)
l=len(numbers)
if len(numbers)%2==0:
median=(new[int(l/2)]+new[int((l-2)/2)])/2
else:
median=new[int((l-1)/2)]
return median
print(median([4,5,5,4]))
When I use the IDLE to calculate,the answer is 4.5.
But the website told me the answer is 4
Can somebody tell me why ?
Thanks!!!


#2

your IDLE runs python3, changes to division behavior where made in python3:

# python2
print 9 / 2 # 4
# python3
print(9/2) # 4.5

to overcome this problem in python2, the division should involve a float


#3

Oh,I see
Thanks a lot :slight_smile:


#4

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