Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i get the output to print 10 circles instead of 9 in python turtle module

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading