 The question:

Create a function called `middle_element` that has one parameter named `lst` .

If there are an odd number of elements in `lst` , the function should return the middle element. If there are an even number of elements, the function should return the average of the middle two elements.

def middle_element(lst):

if len(lst) % 2 == 0:

``````sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]

return sum / 2
``````

else:

``````return lst[int(len(lst)/2)]
``````

#Uncomment the line below when your function is done

print(middle_element([5, 2, -10, -4, 4, 5]))

sum = lst[int(len(lst)/2)] . How I’m getting -4? This topic was not covered in the lesson. The total length of the list is 6 and if i divide it by 2, i get 3 but how i’m getting -4 here? Kindly advice.

If you have a query about a specific lesson then please include a link to that lesson and make sure to format any code you submit to the forums. The following FAQ contains useful guidance about this-

I’m not quite sure how you have wound up with `-4`. The expected answer in Python3 would be `-7.0` (or just `-7` in Python2).

Consider the purpose of the `if` statement. What does `len(lst) % 2` evaluate to for this list (six items). Check how the instructions say the middle element should be calculated in this case.

As an additional note this part of the expression `int(len(lst) / 2)` is calculated at three separate locations. It might be simpler to calculate this once and assign it to a variable to make this function a little more readable.