15/15 not sure whats wrong


#1

The list is [4, 5, 5, 4] and my first term is 4 like it's supposed to be but the second prints out as 3 when it's supposed to be the number 5 at index 3.
def median(numbers):

    #sort
    numbers = sorted(numbers)
    #median values
    term1 = numbers[len(numbers)/2]
    term2 = numbers[(len(numbers)/2)-1]
    print term1
    print term2
    odd_median = numbers[len(numbers)/2]
    #if even
    if len(numbers) % 2 == 0:
        result = term1 + term2 / 2.0
    #if odd
    if len(numbers) % 2 != 0:
        result = odd_median
    return result

#2

Hello :smile:

Problem is in this line;

result = term1 + term2 / 2.0 # term1 + half of term2

You have to remember the order of arithmetic operations. Divisions are before additions, right?

Corrected code:

result = (term1 + term2) / 2.0

#3

Thanks man I knew it'd be something simple like that.


#4

You're very welcome mate :smile: