Hi,
I got stuck on the last assignement on the advanced part of lists.
I wrote the code with the correct functions, all though i tried to use floats in the even-number part.
However, I cannot wrap my head around the part for odd numbers. Let´s assume we have 7 elements in our list, i.e. len(str) is odd. Codecademy suggets we call str[int(len(str)/2)] which i do not understand, since len(str)/2 equals 3.5, but then we force it to become a integer which is resonable.
But if i create a variable i and set it equal to 3.5 then prints it, it will not be 4, as it prints 3.5 when i try it. Code example below. Then how does is manage to call for index 4 and be correct in the example above and fully copied below? Please advise!
i = 3.5
print(int(i))
Output: 3.5
Assignment: https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-lists
#Write your function here
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]))