draw circle using squares

I am checking out a Python course at udacity and found an interesting Python module named Turtle. Now let me share with you a simple code that I just have written.

import turtle

def draw_square():
    window = turtle.Screen()
    window.bgcolor("red")
    
    brad = turtle.Turtle()
    brad.shape("turtle")
    brad.color("yellow")
    brad.speed(5)
    counter = 0
    while counter < 36:
        brad.forward(100)
        brad.right(90)
        brad.forward(100)
        brad.right(90)
        brad.forward(100)
        brad.right(90)
        brad.forward(100)
        brad.right(100)
        counter += 1

    window.exitonclick()

if __name__ == "__main__":
    draw_square()

Just save the code in a file and run it, and you will see something like this: 


Now a simple question for you. How did I come up with the number 36 in the while loop? (while counter < 36).

Comments

Unknown said…
Because you are rotating extra 10° each iteration. So it would take 360/10=36 iteration to complete the circle.
Keyser said…
Each loop with draw a new square rotated 10° to the previous one. So 360/10 = 36 rotations will complete the circle.

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code