我试图用python的乌龟函数递归绘制一个正方形,并在正方形内,递归地在其中绘制四个,在这四个内部,还有四个。 我的代码通过右上角,但返回代码时出错。这是:
import turtle
turtle.speed(0)
def drawSquare(xCenter, yCenter, edgeLength, depthLeft):
xCenter = xCenter
yCenter = yCenter
edgeLength = edgeLength
depthLeft = depthLeft
if depthLeft == 0:
return
else:
turtle.penup()
turtle.goto((xCenter-.5*edgeLength),(yCenter-.5*edgeLength))
turtle.pendown()
for i in range(4):
turtle.forward(edgeLength)
turtle.left(90)
depthLeft = depthLeft - 1
edgeLength = edgeLength/4
drawSquare(xCenter+edgeLength,yCenter+edgeLength,edgeLength, depthLeft)
drawSquare(xCenter-edgeLength,yCenter+edgeLength,edgeLength, depthLeft)
drawSquare(xCenter+edgeLength,yCenter-edgeLength,edgeLength, depthLeft)
drawSquare(xCenter-edgeLength,yCenter-edgeLength,edgeLength, depthLeft)
turtle.exitonclick()
drawSquare(0,0,500,2)
答案 0 :(得分:0)
您的代码似乎没有任何问题。我能够删除turtle.exitonclick()
并使用https://repl.it/languages/python_turtle
我甚至将初始深度调到4
以获得这个可爱的形象
<强>说明强>
这段代码没有做任何事情,你可以删除它
xCenter = xCenter
yCenter = yCenter
edgeLength = edgeLength
depthLeft = depthLeft