使用乌龟的while循环问题

乌龟必须绘制两个重叠的矩形。然后,移到两个矩形的边界之外。乌龟必须开始沿随机方向绘画/移动,直到它位于矩形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)

离开两个矩形的边界之后,它不会采取任何步骤,我不确定为什么。

qq872167458 回答:使用乌龟的while循环问题

对于矩形情况,我并没有真正了解您的逻辑,但是鉴于rec1和rec2对于矩形1和2中的条件为True,则您的解释将是:

while(not rec2)已经足够了,因为它必须在任何时候都可以运行,除非rec2为True。查看可能的逻辑集(rec1,rec2)

(0,0),(0,1),(1,0),(1,1)->(运行),(不要运行),(运行),(不要运行) 所以我们要(0,0)或(1,0)->(X,0)->不是rec2

您希望它在所有情况下都可以运行,除非它在rec2中。简化逻辑叶子(不是rec2)。看看布尔方程。 *这意味着不需要rec1

还要注意,更新rec1和rec2的函数不在while循环中。您可能还希望在其中包含这些内容,否则它们将不会在while循环期间进行更新,因此,如果您确实输入了它们,您将永远不会知道自己在哪里。另外,通过在循环中打印x0,y0的值进行调试,并仔细检查您的逻辑。这是一个经过更改的功能供您尝试。

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 not rec2:
        ngl = random.randint(0,359)
        t.lt(ngl)
        t.fd(dz)
        num_steps+=1
        x0,y0 = t.pos()
        print(x0,y0) # print for debug. 
        rec1 = isInRect(t,h1) #this updates your conditions
        rec2 = isInRect(t,h2)
    print("Turtle takes ",num_steps,"steps before entering the rectangle")

ranWalkRects(tess,100,50,75,30,20)

当然,这是假设您的逻辑是正确的,并且测试工作正常。我建议您调试并尝试将虚拟值替换为x0和y0,以检查您的逻辑和测试是否正常工作。

还要检查是否至少以图形方式初始化了矩形。您从未在上传的脚本中调用矩形。

,

我想象的是比您使用的方法更简单的方法:

def ranWalkRects(t,dz):
    t.pu()
    t.goto(x0,y0)
    t.pd()
    num_steps = 0

    in_rec1 = isInRect(t,h1)
    in_rec2 = isInRect(t,h2)

    while not in_rec1 or in_rec2:
        angle = random.randrange(360)
        t.lt(angle)
        t.fd(dz)
        num_steps += 1

        in_rec1 = isInRect(t,h1)
        in_rec2 = isInRect(t,h2)

    print("Turtle takes","steps before entering the rectangle.")

但是我相信isInRect()也是可疑的,因为它所做的第一件事就是移动乌龟而没有保存其当前位置!将乌龟移到其他位置后,如何判断乌龟是否在矩形中?这可能是数学上的决定,不需要实际移动乌龟。

本文链接:https://www.f2er.com/3168419.html

大家都在问