15. Median


#1



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


median([1]) resulted in an error: list indices must be integers, not float

I really need help here, would apreciate it alot, hope u can take the time.


def median(lizt):
    w = sorted(lizt)
    x = len(w)
    
    if x % 2 == 0:
        med_firstIndex = int(x /2.0)
        med_secondIndex = [med_firstIndex -1]
        
        total_even = (w[med_firstIndex] + w[med_secondIndex])/2.0
        return total_even
    else:
        median_firstIndex = x/2.0
    total_odd = w[median_firstIndex]
    return total_odd


#2

here:

else:
        median_firstIndex = x/2.0

median_fistIndex will be a float, indexes should be integers instead


#3

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