这是您的代码的修改版本,它根据深度改变颜色:
import turtle
import logging
colors = ['green', 'yellow', 'blue']
def drawzig2(depth, size):
if depth == 0:
pass
else:
turtle.pen(pencolor=colors[depth % len(colors)])
logging.error(
"Depth: " + str(depth) + " " +
"Color: " + colors[depth % len(colors)])
turtle.left(90)
turtle.fd(size / 2)
turtle.right(90)
turtle.fd(size)
turtle.left(45)
drawzig2((depth - 1),(size / 2))
turtle.right(45)
turtle.fd(-size)
turtle.left(90)
turtle.fd(-size)
turtle.right(90)
turtle.fd(-size)
turtle.left(45)
drawzig2((depth - 1), (size / 2))
turtle.right(45)
turtle.fd(size)
turtle.left(90)
turtle.fd(size / 2)
turtle.right(90)
drawzig2(3,100)
它开始产生的屏幕截图:
编辑:根据martineu在评论中的大量反馈,我修改了解决方案,使其对OP更容易理解并包括日志记录,运行时应输出如下:
Depth: 3 Color: green
Depth: 2 Color: blue
Depth: 1 Color: yellow
Depth: 1 Color: yellow
Depth: 2 Color: blue
Depth: 1 Color: yellow
Depth: 1 Color: yellow