Median


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#
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
    else:
       return num[i]


#3

here:

if i==0:

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


#4

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


#5

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


#6

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