Practice makes perfect error [Oops, try again]


#1



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

As a result I get this error: Oops, try again. median([1]) returned 6.0 instead of 1

I try this code in PyCharm and it's working, I tried couple of time with different lists and everytime it's getting me the correct result. What's wrong here?


a = [1,3,20,8,4,10]
b = sorted(a)
x = (len(b) // 2)


def median(number):
    if len(b) % 2 == 1:
        return b[x]
        print b[x]

    else:
        y = (b[len(b)/2]+b[(len(b)/2)-1])/2.0
        return y
    print y

print "Median value of list",a, "is:", median(a)


#2

everything we need to calculate the median should be in the function, so when we call the function multiply times:

print "Median value of list",a, "is:", median(a)
print median([4,5,5,4])

#3

So easy. Thanks a lot!


#4

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