Problem in Median


#1



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


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

Could anyone find the problem?


def median(list1):
    list2 = sorted(list1)
    a = 0
    b = 0
    c = 0
    if len(list2) % 2 == 1:
        c = (len(list2) - 1) / 2
        return list2[c]
    elif len(list2) % 2 == 0:
        a = len(list2)/2
        b = a - 1
        return 1/2 * (list2[a] + list2[b])


#2

It seems that I should use 0.5 other than 1/2 ???


#3

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