我如何将输出打印10个圆圈,而不是在Python Turtle模块中打印9个圆圈

发布于 2025-02-12 08:36:52 字数 1312 浏览 0 评论 0原文

我试图弄清楚为什么我的绘画没有所需的最终结果。如您所见,第一行是打印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()

这是输出,因为您可以看到底部行打印了10个圆圈,这就是我想要的,但是以下是排在只有9个圆圈上方的行

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()

This is the output, as you can see the bottom row prints 10 circles which is what i want, however the following rows above only print 9 circles

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素手挽清风 2025-02-19 08:36:53

让我们看看是否可以使用Turtle自己的dot()方法和一些编码样式更改来简化问题:

from turtle import Turtle, Screen
from random import choice

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)
]

screen = Screen()
screen.colormode(255)

dot = Turtle()
dot.hideturtle()
dot.speed('fastest')
dot.penup()

dot.setposition(-270, -350)  # Starting position

for _ in range(10):  # Cycles nested forloop 10 times

    for _ in range(10): # Create 10 circles
        dot.dot(21, choice(COLOURS))
        dot.forward(50)

    dot.goto(-270, dot.ycor() + 40)  # moves turtle up one row with each iteration

screen.exitonclick()

我们不需要迭代变量的值,因此我们提供虚拟。一旦我们意识到colormode()是单数屏幕实例,我们就不需要乌龟的双重导入。 60speed()方法的参数毫无意义,因此让我们拼出我们真正想要的内容。使用dot()我们只需将笔留在UP位置,然后避免使用penup()pendown()调用。

Let's see if we can simplify the problem using turtle's own dot() method and some coding style changes:

from turtle import Turtle, Screen
from random import choice

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)
]

screen = Screen()
screen.colormode(255)

dot = Turtle()
dot.hideturtle()
dot.speed('fastest')
dot.penup()

dot.setposition(-270, -350)  # Starting position

for _ in range(10):  # Cycles nested forloop 10 times

    for _ in range(10): # Create 10 circles
        dot.dot(21, choice(COLOURS))
        dot.forward(50)

    dot.goto(-270, dot.ycor() + 40)  # moves turtle up one row with each iteration

screen.exitonclick()

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 singular Screen instance which we create sooner rather than later. The 60 argument to the speed() method is pointless so let's spell out what we really want. Using dot() we can just leave the pen in the up position and avoid the penup() and pendown() calls.

俯瞰星空 2025-02-19 08:36:52

在第一次为循环运行的“创建10个圆圈”时,笔已经下降了。因此绘制了所有10个圆圈。但是下次发生时(第二行)时,笔向上 - 这是在“向上移动一排”线中进行的。因此,第一个圆圈不会被绘制。因此,您可以通过调用dot.pendown()在将乌龟设置为新的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

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.

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
梦萦几度 2025-02-19 08:36:52

您需要在嵌套循环的嵌套之后更改代码的最后一个块:

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()

这是您将获得的最终结果:

“在此处输入图像描述”

You need to change the last block of code after the nested for loop onto the top of it:

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()

This is the final result you will get:

enter image description here

暖伴 2025-02-19 08:36:52

该脚本使用Python的Turtle模块创建了彩色的10x10点网格。乌龟会逐行进行系统地移动,为每个点选择随机颜色。该脚本有效地定位了乌龟以保持结构化的网格布局。

每个点都使用RGB值的预定列表随机着色,乌龟以保持均匀间隔的网格的方式移动。

import random
import turtle as t

t.colormode(255) 
screen =t.Screen()
tim = t.Turtle()
tim.speed("fastest")  
tim.penup() 

# List of RGB colors
rgb_color = [(235, 234, 231), (234, 229, 232), (236, 35, 108), (221, 231, 237),
             (145, 28, 66), (230, 237, 232), (239, 75, 35), (7, 148, 95),
             (220, 171, 45), (183, 158, 47), (45, 191, 232), (28, 127, 194),
             (254, 223, 0), (125, 192, 78), (85, 27, 91), (243, 218, 56),
             (178, 40, 98), (44, 170, 114), (211, 132, 166), (206, 57, 35)]

# Starting position
tim.goto(-200, 200)  
dot_size = 20
spacing = 50

# Draw 10x10 grid of dots
for row in range(10):  
    for col in range(10):  
        tim.dot(dot_size, random.choice(rgb_color))  
        tim.forward(spacing)  # Move forward for the next dot
    
    # Move turtle to the next row
    tim.backward(spacing * 10)  # Move back to the start of the row
    tim.right(90)
    tim.forward(spacing)
    tim.left(90)

tim.hideturtle()
screen.exitonclick()

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.

import random
import turtle as t

t.colormode(255) 
screen =t.Screen()
tim = t.Turtle()
tim.speed("fastest")  
tim.penup() 

# List of RGB colors
rgb_color = [(235, 234, 231), (234, 229, 232), (236, 35, 108), (221, 231, 237),
             (145, 28, 66), (230, 237, 232), (239, 75, 35), (7, 148, 95),
             (220, 171, 45), (183, 158, 47), (45, 191, 232), (28, 127, 194),
             (254, 223, 0), (125, 192, 78), (85, 27, 91), (243, 218, 56),
             (178, 40, 98), (44, 170, 114), (211, 132, 166), (206, 57, 35)]

# Starting position
tim.goto(-200, 200)  
dot_size = 20
spacing = 50

# Draw 10x10 grid of dots
for row in range(10):  
    for col in range(10):  
        tim.dot(dot_size, random.choice(rgb_color))  
        tim.forward(spacing)  # Move forward for the next dot
    
    # Move turtle to the next row
    tim.backward(spacing * 10)  # Move back to the start of the row
    tim.right(90)
    tim.forward(spacing)
    tim.left(90)

tim.hideturtle()
screen.exitonclick()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文