Python海龟图形 “问题在底部” 从海龟进口* 速度(0) “”“正在初始化递归”“” def init1(): 希德图尔() up() fd(-125) 向下() “”“正在初始化迭代”“” def init2(): 希德图尔() up() 财务总监(250) 向下() “”“使用递归生成正方形”“” def绘图螺旋图iter2(段): “”“draw\u spiral\u iter2:NatNum->NoneType 绘制以“线段”为单位的线段,并向右旋转90度 分段-缓和曲线中的分段数 """ 如果段

Python海龟图形 “问题在底部” 从海龟进口* 速度(0) “”“正在初始化递归”“” def init1(): 希德图尔() up() fd(-125) 向下() “”“正在初始化迭代”“” def init2(): 希德图尔() up() 财务总监(250) 向下() “”“使用递归生成正方形”“” def绘图螺旋图iter2(段): “”“draw\u spiral\u iter2:NatNum->NoneType 绘制以“线段”为单位的线段,并向右旋转90度 分段-缓和曲线中的分段数 """ 如果段,python,recursion,iteration,turtle-graphics,Python,Recursion,Iteration,Turtle Graphics,drawiter2无限循环,原因有二: 整个过程都在一个while True循环中,永远不会终止 执行drawiter2(segment)时,函数会在参数不变的情况下调用自身 将函数定义更改为: """the question is in the bottom""" from turtle import * speed(0) """initializing for recursion""" def init1(): hideturtle() up() fd(-12

drawiter2
无限循环,原因有二:

  • 整个过程都在一个
    while True
    循环中,永远不会终止
  • 执行
    drawiter2(segment)
    时,函数会在参数不变的情况下调用自身
将函数定义更改为:

"""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"""
def drawiter2(段):

如果对我有好处的话。你是怎么运行的?它是做什么的?这和你期望的有什么不同?提供更多信息将使您更容易获得帮助。
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)
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