Median: printed '1' instead of 1


#1



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


Oops, try again. median([1]) returned '1' instead of 1


Why does it print '1'?? When I test it on Spyder, it prints 1, not '1'


def median(numbers):
    num = sorted(numbers)
    leng = len(num) % 2
    if len(num) == 1:
        return str(num)[1:-1]
    elif leng != 0:
        return num[leng]
    else:
        return (num[leng] + num[leng+1]) / 2.0


#2

One way to fix it is:

if len(num) == 1:
        return num[0]

Btw, your code won't pass the test even if you fixed this issue, it has some other issues which you can try to run and see it yourself.


#3

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