龟的螺旋功能 - 我如何让它停止?

时间:2014-04-22 03:30:30

标签: python function recursion turtle-graphics spiral

所以,我正在尝试制作一个在乌龟中制作螺旋的功能。它看起来工作得很好,除了当我想要它下降到一个像素时它停止绘制时,该函数保持绘图和绘图。任何帮助将不胜感激!

  def spiral( initialLength, angle, multiplier ):
    """uses the csturtle drawing functions to return a spiral that has its first segment of length initialLength and subsequent segments form angles of angle degrees. The multiplier indicate how each segment changes in size from the previous one. 
    input: two integers, initialLength and angle, and a float, multiplier
    """

    newLength = initialLength * multiplier

    if initialLength == 1 or newLength ==1:
        up()

    else:
        forward(initialLength)
        right(angle)
        newLength = initialLength * multiplier
        if newLength == 0:
            up()
        return spiral(newLength,angle, multiplier)

1 个答案:

答案 0 :(得分:1)

根据initialLengthmultiplier的值,您的函数很可能永远不会正好1.您可以在此处检查:

if initialLength == 1 or newLength ==1:
    up()

如果它永远不会达到一个,乌龟永远不会停止绘画。

尝试将其更改为:

if initialLength <= 1 or newLength <=1:
    up()

老实说,你可以这样做:

if initialLength <= 1:
    up()

由于initialLengthnewLength基本上是相同的变量,因此它们只有multiplier的一个因子(一个递归深度)。