Median. My code returns right answer but


#1




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


My code seems to work fine, as in this case(4,5,5,4) it actually returns 4.5, but the same error message as above keeps show up.
I don't understand what is wrong with my code.


num = [int(n) for n in raw_input("Enter numbers divided by spaces:").split()]
nums = sorted(num)
def median(nums):
    length = len(nums)
    if length %2 != 0:
        n = (length-1)/2
        k = nums[n]
    else:
        n = length/2
        k = (nums[n-1] + nums[n]) / 2.0
    return k
print(median(nums))


#2

the exercise is going to call the function for validation purposes, in the function itself is no sorting happening.


#4

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