Recursion: simple spiral with python turtle

4.5k views Asked by At

I'm trying to recreate a function spiral() using recursion that takes the parameters initLen (pixel length of first side), N (angle connecting segments), and mult (a float amount indicating how much bigger/smaller each segment should be after each turn - ex: mult = 0.5 means each segment would be half the length of the previous). The drawing should stop when the segment length reaches less than 1 or greater than 500.

example of desired output for spiral( 100, 90, 0.9 ):

desired output

I have attempted this:

def spiral( initLen, N, mult ):

if initLen> 500:
    return
elif initLen< 1:
    return
else:
    pendown()
    forward(initLen)
    left(360.0/N)
    spiral((initLen*mult),angle, mult)

Thinking the only thing changing is segment length (by order of the multiplier) each turn.

My program output for spiral( 20, 90, 0.9 ) is....

bad spiral

not quite similar to above...

Any hints?

2

There are 2 answers

0
Raniz On BEST ANSWER

The example invocation you posted (spiral( 100, 90, 0.9 )) seems to treat the second parameter as the degree of each turn whereas you treat it as the number of turns to make a complete 360 degree turn in your code.

Either change the call to spiral(20, 4, 0.9) or the turn to left(N).

2
JonD On

inside the function you call

forward(initialLength)

but the variable referenced in the function call and in the rest of the function is

initLen

so probably the value of initialLength is static and not what you want.