I get:
File “script.py”, line 5, in middle_element
average_middle_numbers = lst[len(lst)/3] + lst[len(lst)/2]
TypeError: list indices must be integers or slices, not float
By the way i fixed it by putting int() but I dont remember when to put the int() command. can someone make refresh knowledge?
The int() function is a constructor that creates an int instance object. When to use it depends upon what data type will be expected in the circumstance. This is one…
lst[len(lst)/3] + lst[len(lst)/2]
In Python 3, division results in a quotient of type, float. However, above we are subscripting a list which requires integer index references. Floats cannot be used as indices.
If this is Python 3.x then it will give a float result to the division by 2. Skip all the integer casting on the components. Get to the end of the math and cast it then.
Thanks for the response! By “Get to the end of the math and cast it then.” Do you mean casting the entire line as an int? e.g.
instead of:
lst[int(len(lst)/2) + int(len(lst)/2 - 1)/2]
Do:
int(lst[len(lst)/2 + len(lst)/2 - 1/2])
What eventually worked for me was…
(1) creating a variable that summed the two components I previously summed as one argument of the lst
(2) adding in “return” after the “else” I missed that before
I don’t understand why I’m getting an error here (see attached screenshot). The error seems to be about the fact that the indices of a list cannot be floats. However, my second if statement specifies that the code that follows is for indices divisible by two with zero remainder, so they should not result in a float. I don’t understand why my code didn’t work; what am I missing?
Above we can see the gist of the function with repeated elements removed. Note that len(lst) is repeated five times in your code. We can abstract away that expression by assigning its output to a variable.
n = len(lst)
Run once, use the result multiple times. Notice also how many times your code divides by 2? That operation can also be abstracted away.
m = n / 2
As we know that will produce a float quotient but we can cast that to an integer with the int() constuctor.
m = int(n / 2)
For odd length lists m will be the middle index.
[ ] [ ] [ ] [m] [ ] [ ] [ ]
For an even length list m will be the lowest index in the upper half of the list.
So does this mean that adding int() in front of the length always rounds up and not down? For instance, the list length could be 5 in which case divided be by 2 would be 2.5. So int() always convert it to the higher integer i.e. 3 and not 2? Because it is right in the middle
If you’re doing exact math then you shouldn’t be using floats, there would therefore also be no need to convert from float. Stick to integer math. If you end up with a float then the solution isn’t to convert it to int, it’s to not have it in the first place.
Think of int() (when used with floats) as a function that returns the integer portion of the float. For positive numbers, this is the “lower” integer, for negative, the “higher” (i.e., closer to zero in each case.)
I was able to get the correct answer, however it won’t let me proceed. Maybe I didn’t do it in the most straightforward way, however I did get -7. Could anyone please tell me what I’m not getting?