Practice Makes Perfect - Median exercise


#1


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

Hi everybody:
Can anybody, please, tell me what's wrong with my code? I know it doesn't look as one would like, but I'll find out a better way to solve this.
It throws up an error that says what I just posted down here, but it works fine in Python.

Thanks in advance for any light you can shed on this! (:slight_smile:

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


def median(lst):
    sortd_lst = sorted(lst)

    if len(sortd_lst) % 2 == 0:
        print("The list has an even number of items")

        vaca = sortd_lst[len(sortd_lst) // 2]
        first_num = vaca                  
        second_num = vaca - 1                       
        mid = (first_num + second_num) / 2
        
    else:

        print("This list has an odd number of items")

        mid = sortd_lst[(len(sortd_lst) // 2) - 1] 
    print(mid)
    return mid
    print(mid)

median([4, 5, 5, 4])


#2

Wrap the bolded expression in float().


#3

The median for an even set of numbers is the average of the numbers in the middle. The line second_num = vaca - 1 is incorrect for a general median function.