I am attempting to solve the python list challenge 5 using my limited understanding and keep getting an error: “TypeError: list indices must be integers or slices, not float”
Here it the code I attempted –
def middle_element(lst): num = len(lst) if num % 2 != 0: return lst[(num + 1) / 2] else: return lst[num/2] + lst[num/2 + 1] /2 print(middle_element([5, 2, -10, -4, 4, 5]))
The prompt was:
Create a function called
middle_element that has one parameter named
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.
Any help explaining my errors would be much appreciated.