15. median stuck


#1


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

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

to return 4.5


def median(aList):
    renewed=sorted(aList)
    medIndex= (len(renewed)-1)/2
    
    if (medIndex/0.5)%2==0:
        output=renewed[medIndex]
    else:
        medIndex-=0.5
        output=(renewed[medIndex]+renewed[medIndex+1])/2.0
        
    return output


#2

Why on this line:

if (medIndex/0.5)%2==0:

not check if the list has a odd or even numbers of items? That is currently not what you are doing. To check if a list is odd or even simple do:

if len(renewed) %2 == 1: #list is odd

Also, the len of the list divided by two will always return the middle item if the list has a odd number of items, or if the list has a even number of items, the right one of the two middle items. So, len /2 - 1 will return the left item of the middle item.

for example, if this is your list:

[4,4,5,5]

the len is 4, 4 / 2 = 2. At index two, there is 5. So at index 1 there 4. Which are the two middle items, which you need