YMO
YMO

Reputation: 3

IndexError: list index out of range error python

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)

Upvotes: 0

Views: 74

Answers (1)

Manuel Ramirez
Manuel Ramirez

Reputation: 64

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

Upvotes: 3

Related Questions