乌龟必须绘制两个重叠的矩形。然后,移到两个矩形的边界之外。乌龟必须开始沿随机方向绘画/移动,直到它位于矩形1的边界或内部,而不是矩形2的边界。它还应计算并打印在达到之前所执行的步骤数。
我似乎无法使while语句起作用。问题出在函数ranWalkRects(t,x0,y0,x1,y1,w1,h1,x2,y2,w2,h2,dz)
def ranWalkRects(t,x0,y0,x1,y1,w1,h1,x2,y2,w2,h2,dz):
"""has turtle t start at x0,y0 outside of both rectangles.
Execute a random walk until it is inside or on the boundary of rectangle 1 but not
inside or on the boundary of rectangle 2. Assume that rectangle 1 overlaps rectangle
2 but that rectangle 1 does not lie entirely inside rectangle 2. Print out how many
times the turtle moves from its starting position to the final position. Dz is
the step size for the random walk. """
rec1 = isInRect(t,h1)
rec2 = isInRect(t,h2)
t.pu()
t.goto(x0,y0)
t.pd()
num_steps = 0
while (rec1 and rec2) and (not rec1 and not rec2) and not (rec1 and not rec2):
ngl = random.randint(0,359)
t.lt(ngl)
t.fd(dz)
num_steps+=1
x0,y0 = t.pos()
print("Turtle takes ",num_steps,"steps before entering the rectangle")
ranWalkRects(tess,100,50,75,30,20)
离开两个矩形的边界之后,它不会采取任何步骤,我不确定为什么。