Median. My code returns right answer but


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]
        n = length/2
        k = (nums[n-1] + nums[n]) / 2.0
    return k


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


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