Median


#1

need your help,:pray:,I can't figure out why I was wrong,the error goes like that :
oops,try again, median([4, 5, 5, 4]) returned 5 instead of 4.5
my code here:

def median(lvjc):
    donkey=sorted(lvjc)
    l=len(donkey)
    print l
    if l % 2== 0:
        return (donkey[l/2]+donkey[l/2-1])/2
    else:
        return donkey[l/2]

thank you


#2

i get a different error with your code:

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

Either way, lets look at this line of code:

return (donkey[l/2]+donkey[l/2-1])/2

lets say our list is [4, 5, 5, 4], we can actually do the math:

5 + 4 / 2

agree? Problem, a integer (non decimal number) divided by a integer returns a integer. Oops, to overcome this problem, use float() or divide by a integer (2.0), which is the same as 2, except now you will get a float number in return


#4

It worked! thank you so much your ur kind reply!


#5

that's the problem​:heartpulse: THANK u for your reply.