Receiving a data type error even though I feel like my logic is correct

I am getting a data type error:


  length2 = (len(lst) / 2 - 1)
  print(length2)
  position2 = lst[length2]

  length3 = (len(lst) / 2)
  print(length3)
  position3 = lst[length3]

#finds average
  average = (position2 + position3) / 2

Output:
2.0
Traceback (most recent call last):
  File "script.py", line 26, in <module>
    print(middle_element([5, 2, -10, -4, 4, 5]))
  File "script.py", line 12, in middle_element
    position2 = lst[length2]
TypeError: list indices must be integers or slices, not float

However I feel like my code/logic is correct?

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.

#Write your function here
def middle_element(lst):
  length = len(lst)
#takes the length and divides it by 2 to return the middle element
  half_of_length = length / 2
#modulo by 2 to determine if it is odd
  if length % 2 != 0:
    return half_of_length

  length2 = (len(lst) / 2 - 1)
  print(length2)
  position2 = lst[length2]

  length3 = (len(lst) / 2)
  print(length3)
  position3 = lst[length3]

#finds average
  average = (position2 + position3) / 2

#finds if the lst is even
  if length % 2 == 0:
    return average
  
#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))
1 Like

If that is the name of the function, we would expect the value in the middle element, not half of the length. Just saying…

1 Like

The following line of the error says list indices must be integers or slices, not float.

TypeError: list indices must be integers or slices, not float

maybe try casting average into an int like

average =  int(average)

before you return it.

1 Like

That’s what I was thinking as well but I haven’t been taught how to cast integers in the course yet. I have been taught the difference between integers and floats but not necessarily how to cast them.

Isn’t half the length the middle? Aren’t those two the same thing essentially?

well dang look at me go if that is right. I haven’t done their python course yet. I read the big book though.

That does resolve the issue but I’m curious to see if there is another way to go about this to see if my logic was off.

Sick. I’ll let you keep it up to see if someone else resolves it a different way but I’d like the solve points if what I said was right.

1 Like

If you give me a link to the exact thing you’re looking at I can see if I can see anything assuming this is part of some course as I’m a pro member. Just haven’t started that part of their stuff yet, but obviously have some skills.

You are returning half the length, not the middle value.

Yes understood but half is the middle.

Ex. 100/2 = 50 which is also half way. I’m confused, where is the issue with my logic? Anything divided by 2 is the middle. Is there some type of middle function or slice that I can use?

Yeah, but that’s an index. What value is at that index? That’s what we need to return. That value.

  length2 = (len(lst) / 2 - 1)
  print(length2)
  position2 = lst[length2]

  length3 = (len(lst) / 2)
  position3 = lst[length3]

That’s what I’m doing here. The value at that index is being stored in position3 and position2

Is that int or float?

mtf is talking about this line

  if length % 2 != 0:
    return half_of_length

You’re returning the number instead of the array index like

return lst[half_of_length]

Yeah but for the other return statement it is asking to return the average of the middle two numbers. Can I return the average with the array index?

@alangervin3193293788

I’m actually still having issues with this.

It’s saying when I check my answer:

list indices must be integers or slices, not float

#Write your function here
def middle_element(lst):
  length = len(lst)
#takes the length and divides it by 2 to return the middle element
  half_of_length = length / 2
#modulo by 2 to determine if it is odd
  if length % 2 != 0:
    return lst[half_of_length]

  length2 = int(len(lst) / 2 - 1)
  print(length2)
  position2 = lst[length2]

  length3 = int(len(lst) / 2)
  print(length3)
  position3 = lst[length3]

#finds average
  average = (position2 + position3) / 2

#finds if the lst is even
  if length % 2 == 0:
    return int(average)
  
#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/articles/advanced-python-code-challenges-lists

It’s the last one in this list. I also recommend doing these problems again, they’re incredibly challenging.

To return the average of two indexes. you first will need to find those indexes and then apply proper math logic to it. so also length3 should also be the following.

length3 = (len(lst)/2+1)

sorry I was just fixing the first error now I’m seeing more. that’s how I code .

I apply that logic here just opposite:

  length2 = int(len(lst) / 2 - 1)
  print(length2)
  position2 = lst[length2]

instance of 2+1 I did 2-1

so that’s a mess up then. because 6/2 = 3 then 3-1 =2 so you would be adding 2 and -10. you want to be adding -10 and -4. you need length3 and fix length2 to be +1 instead of -1