【发布时间】:2018-09-13 02:32:42
【问题描述】:
我在编写一个将圆圈绘制到某个“深度”的递归函数时遇到问题。
- 例如,在深度一,程序应该绘制这个:depth 1
- 在深度二,程序应该绘制这个:depth 2
- 在深度 3 处,程序应绘制如下:depth 3
- 作为参考,我的程序绘制了这个:myTurtleDrawing
import turtle
# These are basic instructions that do not affect the drawing if changed, only the appearance of the entities within the window
turtle.mode('logo')
turtle.speed(1)
turtle.shape('classic')
turtle.title("Circle")
def recCircle(d, r):
if d == 0:
pass
if d == 1:
print("Drawing to the depth of: ", d)
turtle.down()
turtle.circle(r)
turtle.up()
else:
print("Drawing to the depth of: ", d)
turtle.circle(r)
turtle.seth(90)
turtle.down()
recCircle(d - 1, (r / 2)) # Draw the leftmost circle
turtle.seth(360)
turtle.up
turtle.seth(270)
turtle.forward(2 * r)
turtle.down()
recCircle(d - 1, - r / 2) # Draw the rightmost circle
turtle.up()
turtle.seth(360)
turtle.forward(2*r)
def main():
d = 3 #depth of recursion
r = 100 #radius of circle
recCircle(d, r)
turtle.done()
main()
我认为问题出在第 20 行左右
turtle.circle(r)
我不知道如何将海龟返回到相同的位置和方向。
turtle.home 或 turtle.goto,因为我试图不使用它们
【问题讨论】:
标签: python python-3.x recursion turtle-graphics