0

here is my simple python code. I could not figure out why its shows index out of range .any help .

distances = []
i=0
for i in range(len(points)):
    point = points[i]
    next_point = points[i+1]
    x0 = point[0]
    y0 = point[1]        
    x1 = next_point[0]
    y1 = next_point[1]       
    point_distance = get_distance(x0, y0, x1, y1)        
    distances.append(point_distance)
3
  • What is points? What line gives the index out of range error? Commented Jan 31, 2015 at 1:47
  • next_point = points[i+1] line gives the error points is the list of coordinate vales Commented Jan 31, 2015 at 1:49
  • next_point = points[i+1] IndexError: list index out of range Commented Jan 31, 2015 at 1:50

1 Answer 1

3

I am not sure but maybe it's the fact that you're using points[i+1]. When you reach the last position of points (points[i]) the i+1 will try to access a position that does not exists. You need to check if you're in the last position before getting the next point or limit your for cycle to: len(points) - 1

Sign up to request clarification or add additional context in comments.

Comments

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.