- Modify the condition in the outer loop so the turtle drawing has two “peaks” as shown, instead of one. No other change to the code needs to be made.
# a114_nested_loops_4.py
import time
import turtle as trtl
painter = trtl.Turtle()
painter.penup()
painter.goto(-200, 0)
painter.pendown()
x = -200
y = 0
move_x = 1
move_y = 1
while x < 200:
while y < 100:
x = x + move_x
y = y + move_y
painter.goto(x, y)
move_y = -1
while y > 0:
x = x + move_x
y = y + move_y
painter.goto(x, y)
move_y = 1
# print("dd")
# time.sleep(10)
x = -200
y = 0
move_x = 1
move_y = -1 # 初始y方向改为向下
painter.penup()
painter.goto(-200, 0)
painter.pendown()
while x < 200:
while y > -100: # 变为负值
x = x + move_x
y = y + move_y
print("上:", x, y)
painter.goto(x, y)
move_y = 1 # 现在向上移动
while y < 0:
x = x + move_x
y = y + move_y
print("下:", x, y)
painter.goto(x, y)
move_y = -1 # 恢复向下的方向
wn = trtl.Screen()
wn.mainloop()
原创2023年10月24日...大约 2 分钟