0
import math

def getBestLoc(lst):
    if len(lst) % 2 == 0:
        #bestLoc = (lst((len(lst)/2) + lst(len(lst)/2)-1))/2
        bestLoc = ((lst[len(lst)]//2) + (lst[len(lst)/2]-1)) // 2
    else:
        bestLoc = lst(len(lst)//2)
    sumOfDistances(lst, bestLoc)
    return bestLoc

def sumOfDistances(lst, bestLoc):
    total = 0
    for i in lst:
        total += math.abs(bestLoc - i)
    return total

def main():
    lst = [10, 15, 30, 200]
    lst.sort()
    print(lst)
    getBestLoc(lst)

main()

I got this error:

Traceback (most recent call last):
  File "C:\Users\NAME\Desktop\storeLocation.py", line 32, in <module>
    main()
  File "C:\Users\NAME\Desktop\storeLocation.py", line 30, in main
    getBestLoc(lst)
  File "C:\Users\NAME\Desktop\storeLocation.py", line 14, in getBestLoc
    bestLoc = ((lst[len(lst)]//2) + (lst[len(lst)/2]-1)) // 2
IndexError: list index out of range

I don't know what I did wrong. It is saying, IndexError: list index out of range, I don't know what that means. It is my lab homework. Trying to figure this issue out. Any help? Thanks.

4 Answers 4

4

You need to access an element of the list with [], not to call it as a function with (). Replace:

bestLoc = ((lst(len(lst))/2) + (lst(len(lst)/2)-1)) / 2

with

bestLoc = ((lst[len(lst)]/2) + (lst[len(lst)/2]-1)) / 2
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! I didn't look carefully at the [] and (). Also, When I done that, I get this new error: IndexError: list index out of range I don't get how is this out of range.
Replace lst[len(lst)] with lst[len(lst) - 1] or (better) with lst[-1] to correctly get the last element.
Also, if using Python3, replace / with //, or you might get an error complaining about float.
bestLoc = ((lst[-1]/2) + (lst[len(lst)/2]-1)) / 2 TypeError: list indices must be integers, not float I have no idea what is the means. I know what is Int and Float.
Read the comment above: replace lst[len(lst)/2] with lst[len(lst)//2]
|
2

accessing list element uses square bracket [] not ()

should be

((lst[len(lst)]/2) + (lst[len(lst)/2]-1)) / 2

same thing with the else: clause

2 Comments

Thanks! I didn't look carefully at the [] and (). Also, When I done that, I get this new error: IndexError: list index out of range I don't get how is this out of range.
a = [1,2,3], len(a)=3, but last element of a is a[2], so you have to do a[len(a) - 1] to get the last element
2

You're attempting to use lst as a function by using lst(...). Try using square brackets lst[...].

Comments

1

What other posters have said. Use square brackets [] instead of parentheses () to access list elements. eg. lst[index] Also, you are attempting to access elements outside the list.

Don't use lst[len(lst)] - you automatically get an index out of bounds exception. Instead, use lst[len(lst)-1].

Also, don't use math.abs, that function doesn't exist in python. Instead use math.fabs

2 Comments

Actually, you have abs in Python.
I don't quite understand Don't use.... Which part in the code? I know I got the index out of range

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.