Median


#1

This is the code:
'''
def median(l):
l.sort()
if len(l)%2 != 0:
final = l[len(l)//2]
else:
final = (l[len(l)//2]+l[len(l)//2 - 1])/2
return final
'''
The error is: 'Oops, try again. median([4, 5, 5, 4]) returned 4.0 instead of 4.5'
But, it works on my python console, it gives me 4.5 as result.. can someone help me?
Thank you a lot!


#2

your python console very likely runs python3, where as the exercise runs python2:

# python 2
print 9 / 2 # 4
# python 3
print 9 / 2 # 4.5

as you can see, the division doesn't behave the same

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


#3

I didn't know that! Thank you! Yes, my console runs python 3


#4

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