15. Median - Help, can't go through



Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5

return the correct median

def median(numeros):
    len_numeros = len(numeros)
    if len_numeros%2 == 0:
        medeven = ((len_numeros)/2)
        mediana = (numeros[int(medeven)] + numeros[int(medeven-1)]) /2
    elif len_numeros == 1:
        mediana = 1
        medianaindex = ((len_numeros-1)/2)
        mediana = numeros[int(medianaindex)]
    return mediana
print (median(numeros))


medeven = int(len_numeros / 2)
if len_numeros % 2 == 0:
    mediana = float(numeros[medeven] + numeros[medeven-1]) / 2
    mediana = numeros[medeven]


Make shure you have at least one operand of your division in 'float' type. This automatically force result to be a floating number.

 mediana = (numeros[int(medeven)] + numeros[int(medeven-1)]) /2.0


My code looks similar, can you please explain why it isn't working even though I have included float for the result?

def median(numbers):
    sortlist = sorted(numbers)
    if len(sortlist) % 2 == 1:
        y = len(sortlist) / 2
        x = int(y)
        return sortlist[x]
        a = len(sortlist) / 2
        return float((sortlist[a-1] + sortlist[a]) / 2)
print median([4,4,5,5])

The error is the same, with same example [4,5,5,4] results with 4 instead of 4.5


Cast to float an operand, not result.
c = a / float(b)


Converting the numerator to a float is not a good practice. It is a counting number and should rightly be left as one, rather than massaging it into a float.

The numerator is where the float belongs.

return float(sortlist[a-1] + sortlist[a]) / 2

Good point, as long as we cast the number that is most likely to be a float, not the counting number. Implicit casting is a dangerous road to go down if we want our code to be readable and make sense. Massaging working values never ends well.


Thank you for your help, however can you please explain why it doesn't work when I leave the number "2" as a float too?


Because it is massaging an already internally defined result, which may already be an integer in Python 2, so it stays an integer value, sans any decimal fraction, even while we have cast it as a float. As @systemcoder68263 alluded, cast one of numerator or denomiator, not the quotient.

For this reason, I harp on the retention of any counting numbers in the division. Those values or expressions that do not contain a variable are treated as their own type. An expression that contains a variable may be type cast arbitrarily without disrupting the logic and semantic flow of the program for a reader.

Maybe it's just me. I cringe when I see division by 2.0 or anything point zero. It's just too artificial. I'm glad that Python 3 got away from that and just returns floats, period, without our massaging them. It is now an imperative to declare integers. Finally we get to see the function when it literally applies.


Thank you @mtf for important notice! It really does make sense! I want to know more about readeble code, could you suggest a book or other information sources,please?


If I had an Amazon affiliation I could proably come up with a few... Truth be told, all you need is online. Read the documentation.


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