Python and Turtle custom functions,Difficulty Level 3,for loop,loop,nested for loop Circle Matrix with Python Turtle (with Solution)

Circle Matrix with Python Turtle (with Solution)

In this simple python turtle project, you are going to draw a 10×10 matrix of connected circles. You can either use nested loops or define a function that draws a line of circles and and call this function in a single loop.

Solution

import turtle

def draw_circle(x,y,r):
    turtle.up()
    turtle.seth(0)
    turtle.goto(x,y-r)
    turtle.down()
    turtle.circle(r)

def draw_a_line_of_circles(x,y,r,n):
    for i in range(n):
        draw_circle(x,y,r)
        x += 2*r
    
turtle.setup(900,900)
turtle.title("Circle Matrix - PythonTurtle.Academy")
turtle.speed(0)
turtle.hideturtle()
y = 300
radius=30
for i in range(10):
    draw_a_line_of_circles(-300,y,radius,10)
    y -= radius*2

Related Post