import turtle

def draw_branch(length):
    #绘制右侧树枝
  if length >5:
    if length == 10:
          turtle.pencolor('green')
    turtle.forward(length)
    turtle.right(20)
    draw_branch(length-15)
    #绘制左侧树枝
    turtle.left(40)
    draw_branch(length-15)
    #返回之前树枝
    turtle.right(20)
    turtle.backward(length)
    if length == 10:
        turtle.pencolor('yellow')
    else:
        turtle.pencolor('red')
def main():

    turtle.pencolor('red')
    turtle.pensize(5)
    turtle.left(90)
    turtle.penup()
    turtle.backward(150)
    turtle.pendown()
    draw_branch(100)
    turtle.exitonclick()
if __name__=='__main__':
    main()

 

python 画出语法树 用python画树枝_python 画出语法树