分形树是采用递归的思想绘制的一种图形。以下为绘制代码,递归思想打算放在另一篇随笔。
import turtledef draw_tree(length): if length > 5: turtle.forward(length) print("前进了",length) turtle.right(20) print("向右转了20度") draw_tree(length-15) turtle.left(40) print("向左转了40度") draw_tree(length-15) turtle.right(20) print("向右转了20度") turtle.backward(length) print("向后退了",length)def main(): turtle.left(90) turtle.penup() turtle.backward(80) turtle.pendown() draw_tree(100) turtle.exitonclick()if __name__=='__main__': main()