Open In App

Printing pattern with Recursion using Python Turtle

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Pre-requisites: Turtle Programming in Python

In this article, we are going to display the following pattern using Python Turtle with recursion.

In this pattern, the first circle should start with 100-pixel radius, and each subsequent circle is 5 pixels smaller until it reaches radius 10 when the last circle is indrawn. The first circle is drawn with a red pen, then green, then yellow, then blue.

Approach:

  • Iterate through a list of colors red, green, yellow, blue.
  • Set the pen color
  • Call a function to draw a circle with a radius size set to 100, the function will recursively call itself until it reaches size 10, which will be the last circle.
  • Turn 90 degrees to the right.

Below is the implementation:

[tabby title="Python3"][sourcecode language="python3"]import turtle # Recursive function to print # circles with reducing radius def printCircle(r): # If radius is less than 10 if r < 10: return else: # function to draw circle # with radius r turtle.circle(r) # function calling itself # with radius decreased by 5 printCircle(r-5) # list of colors colors = ["red", "green", "yellow", "blue"] # printing circles for each color for c in colors: # setting stroke color of circle turtle.color(c) printCircle(100) # turning the pen 90 degrees to right turtle.right(90) # (optional) # To stop the window from closing input() [/sourcecode][tabbyending]

Output:


Next Article
Article Tags :
Practice Tags :

Similar Reads