Pracitce Makes Perfect - median


#1

Hello, I need a help
I can't find a mistake in my script, and it doesn't work in codecademy, but work well in IDLE.
My script

def median(medlist):
    medlistsort = sorted(medlist)
    print medlistsort
    if len(medlistsort) < 2:
        return medlist[0]
    elif len(medlistsort) > 1:
        if len(medlistsort) % 2 == 0:
            mod = int((len(medlistsort) / 2) - 1)
            print mod
            lin = int(mod + 1)
            print lin
            print medlistsort[mod]
            print medlistsort[lin]
            med1 =((medlistsort[mod] +  medlistsort[lin])/ 2)
            print med1
            return med1
        else:
            mid = int(((len(medlistsort)) - 1) / 2)
            med2 = (medlistsort[mid])
            print med2
            return med2
median([4,5,5,4])

#2

Well, on this line:

med1 =((medlistsort[mod] +  medlistsort[lin])/ 2)

you divide the sum of two integers by an integers, that will return a integer. You either need to cast it to a float (using float() ) you could change 2 to 2.0, dividing by a float will return a float


#3

Thank you stetim94
it works, great :smile: