Why this code is not working?


#1



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

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

What is the value of k? I though it would be 9 and if it is divided by 2 then it should give 4.5 for the value of m.

why m is 4 but not 4.5 in this case?


def median(a):
    b=sorted(a)
    if len(b) % 2 == 0: # if it is a even set
        one=len(b)/2
        two=one-1
        k=b[one]+b[two]
        m=k/2
        return m
    elif len(b)==1:
        return b[0]
    else:
        return b[(len(b)/2)]


#2

because is python2, dividing two integers gives a integer as result (if needs be, python floors the numbers), you could solve this problem by dividing by 2.0 which is a float, this way you will get a float as result


#3

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