Median: printed '1' instead of 1


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]
        return (num[leng] + num[leng+1]) / 2.0


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.


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