Question

"""the question is in the bottom"""

from turtle import *

speed(0)
"""initializing for recursion"""
def init1():
    hideturtle()
    up()
    fd(-125)
    down()
"""initializing for iteration"""
def init2():
    hideturtle()
    up()
    fd(250)
    down()
"""Making squares using recursion"""
def draw_spiral_iter2(segment):
    """ draw_spiral_iter2: NatNum -> NoneType
        Draws a line segment of 'segments' units and turns right 90 degrees
        segments - The number of segments in the spiral
    """
    if segment<=5:
        pass
    else:
        up()
        fd(segment/2)
        right(90)
        down()
        fd(segment/2)
        right(90)
        fd(segment)
        right(90)
        fd(segment)
        right(90)
        fd(segment)
        right(90)
        fd(segment/2)
        left(90)
        up()
        fd(-segment/2)
        draw_spiral_iter2(segment*.7)
"""using iteration"""
def drawiter2(segment):
    while True:
        if segment <= 5:
            break
        else: 
            up()
            fd(segment/2)
            right(90)
            down()
            fd(segment/2)
            right(90)
            fd(segment)
            right(90)
            fd(segment)
            right(90)
            fd(segment)
            right(90)
            fd(segment/2)
            left(90)
            up()
            fd(-segment/2)
            drawiter2(segment-segment*.3)
            drawiter2(segment)


def MAX_SEGMENT():
    """ The MAX_SEGMENT() constant is the maximum length of line segment.
    """
    return 200     # should be strictly greater than 0



def main():
    init1()
    print("iterative, while-loop drawing 2...")
    draw_spiral_iter2( MAX_SEGMENT() )
    input("Hit enter to continue.")
    init2()
    drawiter2( MAX_SEGMENT() )
    input("Hit enter to close window.")
    bye()

main()
"""Recursion is fine but iteration doesn't work properly"""

Question:

It doesn't ask me to press enter to close the window in the end? What should I change?

Était-ce utile?

La solution

drawiter2 endlessly loops for two reasons:

  • The whole thing is in a while True loop that never terminates.
  • The function calls itself with its parameters unchanged when you do drawiter2(segment).

Change your function definition to:

def drawiter2(segment):
    if segment <= 5:
        return
    else: 
        up()
        fd(segment/2)
        right(90)
        down()
        fd(segment/2)
        right(90)
        fd(segment)
        right(90)
        fd(segment)
        right(90)
        fd(segment)
        right(90)
        fd(segment/2)
        left(90)
        up()
        fd(-segment/2)
        drawiter2(segment-segment*.3)

Or, the iterative form:

def drawiter2(segment):
    while True:
        if segment <= 5:
            break
        else: 
            up()
            fd(segment/2)
            right(90)
            down()
            fd(segment/2)
            right(90)
            fd(segment)
            right(90)
            fd(segment)
            right(90)
            fd(segment)
            right(90)
            fd(segment/2)
            left(90)
            up()
            fd(-segment/2)
            segment -= segment * .3
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top