0
Question No.25.3 : finding mid-element in python - not successful. What's the problem in my code?
My Answer: items = [2, 4, 6, 8, 10, 12, 14] #your code goes here length = len(items) mn = length // 2 midEle = items[mn] print(midEle)
4 odpowiedzi
+ 2
No Slick that's not the point.
I tryed it out.
The point is, that it is not ask for the value of the mid position!
Only the position/Index is asked.
In the example prog here above, print(mn) is the solution.
+ 1
Even this doesn't work out:
length = len(items)
mn = length // 2
mn2 = mn+1
midEle = items[mn2]
print(midEle)
But in list, as it starts from 0, 3 is suppossed to be the mid element right?
+ 1
Thank you very much Coding Cat.
Now I got it.
0
7 elements
you divide 7 by 2. In hard division that's 3.
4 is the middle element, not 3.
I suggest the same code, but you set 'nm' to (length // 2) + 1