Need help with "15. Median"


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>
Hi all. I’ve hit a bit of a roadblock with this exercise. I am receiving the error “list index out of range” with the attached code. It seems one of my week spots is indexes, and I don’t appear to fully understand their use. Any help is greatly appreciated.

```python

def median(lst):
s = sorted(lst)
if len(lst) == 1:
return lst[0]
elif len(s) % 2 != 0:
j = (len(s) // 2) + 1
return lst[j]
else:
k = lst[len(s) / 2]
h = lst[k + 1]
return (k + h) / (2.0)

<do not remove the three backticks above>

#2

I figured it out. I was correct in that it was the indexes that were to blame. I neglected to remember that they start with 0 not 1…oops. my final code is below.