15. Median . Help!


#1



Hey there! I'm stuck on 15.Median. Link: https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


I keep getting the message "Oops, try again. median([1]) resulted in an error: unsupported operand type(s) for %: 'list' and 'int'"

I can't quite seem to figure out what's wrong with my code, although I suspect it's something to do with the syntax on lines 6 and 8. I can't work out how to word what I want to get done.

Replace this line with your code. 
Here is my code:
def median(lst):
    result = 0
    even = len(lst) / 2
    lst.sort()
    if lst % 2 == 0:
        result = ([even(i) - 1] + [even(i)]) / 2
    elif lst % 2 != 0:
        result = (even(i - 0.5))
    return result


I'd be really grateful for any help! Thank you.


#2

the problem is here:

result = (even(i - 0.5))

i don't see any square brackets (to access the lst

actually, if the list has an odd numbers of items, the indexes are a perfect match:

len index
1/2=0
3/2=1
5/2=2

the indexes are perfectly in the middle if you divide the length by 2. no need to do minus 0.5


#3

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