# 15/15 median

#1

The following is my code:

``````def median(sequence):
sort = sorted(sequence)
print(sort)
if len(sequence)%2 ==0:
print ((sort[int(len(sequence)/2-1)]+sort[int(len(sequence)/2)])/2)
elif len(sequence)==1:
return sort[0]
else:
return sort[int(len(sequence)/2)+1]

median([4,5,5,4])
``````

When i input it into the codecademy compiler, it returned with the value 4 instead of 4.5

when i input it into pyscripter, it returned 4.5

Is there any issue with my code, or is it just a problem with the codecademy compiler?

#2

While practically all languages are compiled to some degree, python is generally considered interpreted because it does not create code that the cpu can run directly. A program called a virtual machine is used instead, it acts like a cpu but reads a different set of instructions (and those instructions are what python is compiled to, but it’s really just turning text into something more machine-readable

Division for integers in python2 is integer division (the result is an int, therefore no decimals, they are truncated)
In python3 division produces a float, even between two integers

Therefore what you would do in python2 is to convert at least one of the operands to float before dividing

#3

Also, you probably shouldn’t be doing floating point operations when computing indexes, because floats are approximations but your indexes are exact, there should be no approximating involved. (So your conversions from float to int in order to be able to use them as indexes is somewhat questionable)

Exact operations for computing indexes, floating point division when computing the average

#4

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