1

所以,我正在尝试制作一个在海龟中制作螺旋的功能。它似乎工作正常,除了该函数在我希望它下降到一个像素时停止绘制时会继续绘制和绘制。任何帮助,将不胜感激!

  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)
4

1 回答 1

1

根据 和 的值initialLengthmultiplier您的函数很可能永远不会正好为 1。您可以在这里检查:

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

如果它永远不会精确到 1,那么海龟将永远不会停止绘制。

尝试将其更改为:

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

老实说,你可以这样做:

if initialLength <= 1:
    up()

因为initialLengthnewLength本质上是相同的变量,所以它们仅相差一个因子multiplier(一个递归深度)。

于 2014-04-22T04:09:07.300 回答