turtle triangle fun

Just Wrote this piece of code. First just copy-paste the code, run and have fun. Then try to write it yourself and have more fun. :)

import turtle

def draw_triangle(side_length, depth):
    if depth == 0:
        return
    
    counter = 0
    while counter < 3:
        counter += 1
        brad.forward(side_length/2)
        if depth > 1:
            brad.left(120)
        draw_triangle(side_length/2, depth-1)
        brad.forward(side_length/2)
        
        brad.right(120)
        
    brad.left(240)
    
    

if __name__ == "__main__":
    window = turtle.Screen()
    window.bgcolor("white")
    
    brad = turtle.Turtle()
    brad.shape("turtle")
    brad.color("green", "green")
    brad.speed(5)
    
    brad.begin_fill()
    brad.right(120)
    brad.forward(128)
    brad.left(120)
    brad.forward(256)
    brad.left(120)
    brad.forward(256)
    brad.left(120)
    brad.forward(128)
    brad.left(120)
    brad.end_fill()
    brad.color("green", "white")
    brad.begin_fill()
    draw_triangle(128, 3)
    brad.end_fill()
    
    window.exitonclick()

Comments

sipiatti said…
Traceback (most recent call last):
File "turtle.py", line 23, in
window = turtle.Screen()
AttributeError: 'module' object has no attribute 'Screen'

What python version you use? I tried 2.7.6 and 3.4 (linuxmint).
Tamim Shahriar said…
I used 2.7.6. Did you install the turtle package?
sipiatti said…
Yep sorry, my fault. I named the file as turtle.py so it tried to import itself :) I renamed to turtle1.py and it workwd. Thanks for your reply.

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