在第一次为循环运行的“创建10个圆圈”时,笔已经倒下。因此绘制了所有10个圆圈。但是下次发生时(第二行)时,笔向上 - 这是在“向上移动一排”线中进行的。因此,第一个圆圈不会被绘制。因此,您只需确保将乌龟设置为新的X,y坐标后通过致电
dot.pendown()
来确保绘制。
for cycle in range(1, 11): # Cycles nested forloop 10 times
for num in range(1, 11): # Create 10 circles
dot.showturtle()
dot.color(random.choice(colours))
dot.forward(1)
dot.penup()
dot.forward(50)
dot.pendown()
dot.penup()
dot.sety((ypos + 40*cycle)) # moves turtle up one row with each iteration
dot.setx(-270) # Sets the turtle to starting X coordinate with each iteration
dot.pendown() # <<<<<<<<<--------- New addition
您需要在嵌套循环到其顶部之后更改最后一个代码:
for
这是您将获得的最终结果:
from turtle import Turtle, Screen import turtle import random colours = [(232, 251, 242), (198, 12, 32), (250, 237, 17), (39, 76, 189), (38, 217, 68), (238, 227, 5), (229, 159, 46), (27, 40, 157), (215, 74, 12), (15, 154, 16), (199, 14, 10), (242, 246, 252), (243, 33, 165), (229, 17, 121), (73, 9, 31), (60, 14, 8)] turtle.colormode(255) dot = Turtle() dot.color(255, 255, 255) # Hide turtle trail dot.setposition(-270, -350) # Starting position dot.pensize(21) dot.shape("circle") dot.speed(60) xpos = dot.xcor() # X coordinates ypos = dot.ycor() # Y coordinates for cycle in range(11): dot.penup() dot.sety((ypos + 40 * cycle)) dot.setx(-320) for num in range(11): dot.showturtle() dot.color(random.choice(colours)) dot.forward(1) dot.penup() dot.forward(50) dot.pendown() dot.hideturtle() screen = Screen() screen.exitonclick()