Question

So, I am trying to make a function to make a spiral in turtle. It seems to be working fine except for that the the function keeps drawing and drawing when I would like it stop drawing when it gets down to one pixel. Any help would be appreciated!

  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)
Était-ce utile?

La solution

Depending on the values of initialLength and multiplier, it is very possible that your function will never be exactly 1. You check for this right here:

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

If it never reaches exactly one, the turtle will never stop drawing.

Try changing it to:

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

Honestly, you could just do:

if initialLength <= 1:
    up()

Because initialLength and newLength are the essentially the same variable, they only differ by one factor of multiplier (one recursion depth).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top