This's my code, i don't know why median([1]) out of range?

Oops, try again. median([1]) resulted in an error: list index out of range

def median(num):
    num = sorted(num)
    i = len(num)%2
    if i==0:
        b = (num[i]+num[i-1])/2.0
        return b
       return num[i]



if i==0:

you check if the list contains an equal number of items.


sorry, i'm still confused. what's different between my code in line 3&4 and if len(num)%2 ==0 ?


but you try to do two things with i: calculate the middle number and determine if the list contains an even/odd numbers of items. You can't do both


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