Home > Software engineering >  Python turtle: How can I make the turtle go to a point in a list of points after it goes through the
Python turtle: How can I make the turtle go to a point in a list of points after it goes through the

Time:11-21

For example, point 1 connects to point 9, then point 2 connects to point 10 and so on for len(pointList).

I am currently stuck because once the turtle attempts to access the 32nd point, it gets an out of bounds error because the point it would need to connect to is point 0 but it wont do so with my current code.

Each point will need to connect to the 9th point ahead of it for the length of the list (40 in this case because of the text file line amount)

I am hinted to use % operator, but I do not know how I would use it.

code

from turtle import *

speed(10)
fileName = input("What is the name of the file? ")
file = open(fileName, 'r', encoding='UTF-8')

pointList = []
penup()

for line in file:
    lineTokens = line.split()
    
    x = float(lineTokens[0])
    y = float(lineTokens[1])

    point = (x,y)

    pointList.append(point)


def drawPoints():
    for point in pointList:
        goto(point)
        dot(5, "black")
        
drawPoints()

i = 0

for point in range(len(pointList)):

        print(point)
        pencolor("red")
        goto(pointList[i])
        down()
        goto(pointList[i   9])
        up()
        i  = 1

Text file I am reading from to collect points (if needed)

31.286893008046174 -197.53766811902756
61.80339887498949 -190.21130325903073
90.79809994790936 -178.20130483767358
117.55705045849463 -161.80339887498948
141.4213562373095 -141.4213562373095
161.80339887498948 -117.55705045849463
178.20130483767355 -90.79809994790936
190.21130325903067 -61.803398874989504
197.5376681190275 -31.286893008046214
199.99999999999994 -6.394884621840902e-14
197.5376681190275 31.286893008046086
190.21130325903067 61.80339887498938
178.20130483767352 90.79809994790924
161.80339887498945 117.55705045849447
141.42135623730948 141.42135623730934
117.55705045849464 161.8033988749893
90.7980999479094 178.20130483767338
61.80339887498957 190.2113032590305
31.28689300804629 197.5376681190273
1.5987211554602254e-13 199.99999999999974
-31.286893008045972 197.5376681190273
-61.80339887498924 190.21130325903047
-90.79809994790908 178.20130483767332
-117.55705045849432 161.80339887498926
-141.42135623730914 141.42135623730928
-161.80339887498906 117.55705045849444
-178.20130483767312 90.79809994790921
-190.21130325903022 61.80339887498938
-197.53766811902702 31.286893008046114
-199.99999999999946 -5.329070518200751e-15
-197.53766811902702 -31.286893008046125
-190.2113032590302 -61.803398874989384


-178.20130483767304 -90.7980999479092
-161.80339887498897 -117.5570504584944
-141.421356237309 -141.42135623730923
-117.55705045849416 -161.80339887498914
-90.79809994790895 -178.20130483767318
-61.80339887498913 -190.21130325903027
-31.286893008045876 -197.53766811902707
2.327027459614328e-13 -199.99999999999952

CodePudding user response:

You can use the following code:

goto(pointList[(i   9)%len(pointList)])

What this does is get a zero if the expression i 9 evaluates to len(pointList)

Another less elegant solution is:

if i   9 < len(pointList):
    goto(pointList[i   9])
else:
    goto(pointList[0])
  • Related