while循环使用tu的问题

2024-09-28 21:30:21 发布

您现在位置:Python中文网/ 问答频道 /正文

海龟必须画两个重叠的矩形。然后,移动到两个矩形的边界之外。海龟必须开始在任意方向上画画或移动,直到它在矩形一的边界或内部,而不是矩形二。它还应该计算并打印到达它之前所采取的步骤数。你知道吗

我似乎无法使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,x1,y1,w1,h1)
    rec2 = isInRect(t,x2,y2,w2,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,100,50,50,100,75,30,30,100,75,20)

离开两个矩形的边界后,它不会采取任何步骤,我也不知道为什么。你知道吗


Tags: andthenotstepsw1边界x1矩形
2条回答

我不太明白你对于矩形的逻辑,但是假设rec1和rec2对于矩形1和2中的条件是真的假。你的解释是:

while(不是rec2)已经足够了,因为它必须在任何情况下运行while,rec2为真的情况除外。查看您的逻辑集(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,x1,y1,w1,h1)
    rec2 = isInRect(t,x2,y2,w2,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,x1,y1,w1,h1) #this updates your conditions
        rec2 = isInRect(t,x2,y2,w2,h2)
    print("Turtle takes ", num_steps,"steps before entering the rectangle")

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

当然,这是假设您的逻辑是正确的,并且测试工作正常。我建议您调试并尝试用伪值代替x0和y0,以检查您的逻辑和测试是否正常工作。你知道吗

还要检查你是否初始化了矩形,至少是图形化的。你从未在上传的脚本中调用矩形。你知道吗

我设想了一种比您使用的更简单的方法:

def ranWalkRects(t, x0, y0, x1, y1, w1, h1, x2, y2, w2, h2, dz):
    t.pu()
    t.goto(x0, y0)
    t.pd()
    num_steps = 0

    in_rec1 = isInRect(t, x1, y1, w1, h1)
    in_rec2 = isInRect(t, x2, y2, w2, 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, x1, y1, w1, h1)
        in_rec2 = isInRect(t, x2, y2, w2, h2)

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

但是我相信isInRect()也是可疑的,因为它做的第一件事就是移动海龟而不保存它当前的位置!一旦你把海龟移到别的地方,你怎么知道它是否在矩形里?这可能是一个数学决定,不需要实际移动海龟。你知道吗

相关问题 更多 >