【发布时间】:2023-06-23 03:47:01
【问题描述】:
"""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"""
问题:
最后不让我按回车关闭窗口吗?我应该改变什么?
【问题讨论】:
-
对我有用。你是怎么运行这个的?
-
它有什么作用?这与您的预期有何不同?提供更多信息将更容易为您提供帮助。
标签: python recursion iteration turtle-graphics