【问题标题】:Python turtle: How can I make the turtle go to a point in a list of points after it goes through the listPython乌龟:如何让乌龟在通过列表后转到点列表中的一个点
【发布时间】:2021-11-21 01:58:24
【问题描述】:

例如,点 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

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    您可以使用以下代码:

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

    如果表达式 i + 9 的计算结果为 len(pointList),则会得到零

    另一个不太优雅的解决方案是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      相关资源
      最近更新 更多