简体   繁体   中英

Spiral Function for Turtle - how do I make it stop?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM