我如何将输出打印10个圆圈,而不是在Python Turtle模块中打印9个圆圈
我试图弄清楚为什么我的绘画没有所需的最终结果。如您所见,第一行是打印10个圆圈,但以下行只打印9个。请查看我的代码和附件的图像。
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)
ypos = dot.ycor() # Y coordinates
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.hideturtle()
screen = Screen()
screen.exitonclick()
I'm trying to figure out why my painting is not having the desired final outcome. As you can see, the first row is printing 10 circles but then the following rows are only printing 9. Have a look at my code, and the attached image.
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)
ypos = dot.ycor() # Y coordinates
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.hideturtle()
screen = Screen()
screen.exitonclick()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让我们看看是否可以使用Turtle自己的
dot()
方法和一些编码样式更改来简化问题:我们不需要迭代变量的值,因此我们提供虚拟。一旦我们意识到
colormode()
是单数屏幕
实例,我们就不需要乌龟的双重导入。60
对speed()
方法的参数毫无意义,因此让我们拼出我们真正想要的内容。使用dot()
我们只需将笔留在UP位置,然后避免使用penup()
和pendown()
调用。Let's see if we can simplify the problem using turtle's own
dot()
method and some coding style changes:We don't need the values of the iteration variables so we supply a dummy instead. We don't need the double import of turtle once we realize that
colormode()
is a method of the singularScreen
instance which we create sooner rather than later. The60
argument to thespeed()
method is pointless so let's spell out what we really want. Usingdot()
we can just leave the pen in the up position and avoid thepenup()
andpendown()
calls.在第一次为循环运行的“创建10个圆圈”时,笔已经下降了。因此绘制了所有10个圆圈。但是下次发生时(第二行)时,笔向上 - 这是在“向上移动一排”线中进行的。因此,第一个圆圈不会被绘制。因此,您可以通过调用
dot.pendown()
在将乌龟设置为新的X,y坐标后通过调用dot.pendown()
。In the first time the "create 10 circles" for loop runs, the pen is already down. So all 10 circles are drawn. But the next time it happens (2nd row), the pen is up - this was made in the "move up one row" line. So the first circle does not get drawn. Therefore, you can just ensure that it gets drawn by calling
dot.pendown()
after setting the turtle to the new x, y coordinates.您需要在嵌套循环的嵌套
之后更改代码的最后一个块:
这是您将获得的最终结果:
You need to change the last block of code after the nested
for
loop onto the top of it:This is the final result you will get:
该脚本使用Python的Turtle模块创建了彩色的10x10点网格。乌龟会逐行进行系统地移动,为每个点选择随机颜色。该脚本有效地定位了乌龟以保持结构化的网格布局。
每个点都使用RGB值的预定列表随机着色,乌龟以保持均匀间隔的网格的方式移动。
This script creates a colorful 10x10 dot grid using Python's turtle module. The turtle moves systematically row by row, selecting random colors for each dot. The script efficiently positions the turtle to maintain a structured grid layout.
Each dot is randomly colored using a predefined list of RGB values, and the turtle moves in a way that maintains an evenly spaced grid.