例如,點 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
uj5u.com熱心網友回復:
您可以使用以下代碼:
goto(pointList[(i 9)%len(pointList)])
如果運算式 i 9 的計算結果為 len(pointList),則這樣做會得到零
另一個不太優雅的解決方案是:
if i 9 < len(pointList):
goto(pointList[i 9])
else:
goto(pointList[0])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361613.html
上一篇:CPPythonLogicConfusiononModernArt(BronzeUSACO2017USOpenp.3)
