python - turtle 的螺旋功能 - 如何让它停止?

标签 python function recursion turtle-graphics spiral

所以,我正在尝试创建一个函数来在 turtle 中制作螺旋。它似乎工作得很好,除了当我希望它在下降到一个像素时停止绘制时,该函数会继续绘制和绘制。任何帮助将不胜感激!

  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)

最佳答案

根据 initialLengthmultiplier 的值,您的函数很可能永远不会恰好为 1。您检查一下就在这里:

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

如果它永远不会恰好达到 1, turtle 将永远不会停止绘画。

尝试将其更改为:

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

老实说,你可以这样做:

if initialLength <= 1:
    up()

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

关于python - turtle 的螺旋功能 - 如何让它停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23209850/

相关文章:

javascript - 使用 Jinja 动态更新 Javascript 变量?

python - 当脚本位于子目录中时无法导入 numpy

function - 如何将 xargs 与 fish shell 中的函数一起使用?

java - 函数的开始和停止时间不断返回 0 或 1

python - Python 中列表与树的递归应用

python - 操作错误 : unable to open database file: sqlalchemy

python - 从命令行将列表传递给 Python

Javascript - 将参数添加到作为参数传递的函数

c - 如何从char函数返回字符串

algorithm - 在递归方法中绘制调用堆栈