Pls Help! where's the mistake !? - MEDIAN


#1


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?

It gives the error that "median[4,5,5,4] returned 4 instead of 4.5"

I don't see any mistake in my code so what's the reason for this error ??

Replace this line with your code. 

def median(raw_data):
    data = sorted(raw_data)
    countdown = 0
    for i in data:
        countdown += 1
    return countdown
    if countdown % 2 == 0:
        num1 = (countdown / 2) - 1
        num2 = num1 + 1
        inter = data[num1] + data[num2]
        final = float(inter / 2)
        return final
    else:
        num = ((countdown + 1) / 2) - 1
        return data[num]


#2

a function ends, the moment a return keyword is reached, which in your case is happen rather quickly:

return countdown

i recommend to remove it, since it doesn't seem to serve a purpose.

Then there is a problem here:

final = float(inter / 2)

float will cast the result of inter/2 (in case of the error 9 / 2 which is 4) into a float, which becomes 4.0

You might want to use float slightly differently, if you need more help let me know


#3

Thanks!! Now it works