Python 乌龟:如何让乌龟在遍历点列表后到达点列表中的一个点
Python turtle: How can I make the turtle go to a point in a list of points after it goes through the list
例如,点 1 连接到点 9,然后点 2 连接到点 10,以此类推 len(pointList)
。
我目前卡住了,因为一旦乌龟试图访问第 32 个点,它就会出现越界错误,因为它需要连接到的点是点 0,但它不会用我当前的代码这样做。
每个点都需要连接到列表长度前面的第 9 个点(在本例中为 40,因为文本文件行数)
有人提示我使用 %
运算符,但我不知道如何使用它。
代码
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
我正在阅读以收集积分的文本文件(如果需要)
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
您可以使用以下代码:
goto(pointList[(i + 9)%len(pointList)])
如果表达式 i + 9 的计算结果为 len(pointList)
,这会得到一个零
另一个不太优雅的解决方案是:
if i + 9 < len(pointList):
goto(pointList[i + 9])
else:
goto(pointList[0])
例如,点 1 连接到点 9,然后点 2 连接到点 10,以此类推 len(pointList)
。
我目前卡住了,因为一旦乌龟试图访问第 32 个点,它就会出现越界错误,因为它需要连接到的点是点 0,但它不会用我当前的代码这样做。
每个点都需要连接到列表长度前面的第 9 个点(在本例中为 40,因为文本文件行数)
有人提示我使用 %
运算符,但我不知道如何使用它。
代码
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
我正在阅读以收集积分的文本文件(如果需要)
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
您可以使用以下代码:
goto(pointList[(i + 9)%len(pointList)])
如果表达式 i + 9 的计算结果为 len(pointList)
,这会得到一个零另一个不太优雅的解决方案是:
if i + 9 < len(pointList):
goto(pointList[i + 9])
else:
goto(pointList[0])