import turtle
def koch(size, n):
if n == 0:
turtle.fd(size)
else:
for angle in [0, 60, -120, 60]:
turtle.left(angle)
koch(size / 3, n - 1)
def main():
turtle.setup(800, 400)
turtle.penup()
turtle.goto(-300, -50)
turtle.pendown()
turtle.pensize(2)
koch(600, 3)
turtle.hideturtle()
turtle.done()
main()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b8bf696607da95ed615ec59cc5cdab99.png)
import turtle
def koch(size, n):
if n == 0:
turtle.fd(size)
else:
for angle in [0, 60, -120, 60]:
turtle.left(angle)
koch(size / 3, n - 1)
def main():
turtle.setup(600, 600)
turtle.penup()
turtle.goto(-200, 100)
turtle.pendown()
turtle.pensize(2)
level = 3
koch(400, level)
turtle.right(120)
koch(400, level)
turtle.right(120)
koch(400, level)
turtle.hideturtle()
turtle.done()
main()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2b5abef00e03365407e57b88b2b60d09.png)