简体   繁体   中英

Recursion: simple spiral with python turtle

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

期望的输出

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

坏螺旋

not quite similar to above...

Any hints?

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

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.

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