由于多个变量,比预期的循环更长

时间:2013-09-05 21:27:16

标签: python function loops python-2.7

我正在创建一个程序来绘制网格中的随机移动,其中起点是网格的中间位置,用户给出了列数和行数。目前的问题是我有两个变化的变量,都需要发出程序结束的信号。 X用于水平移动,Y用于垂直移动:如果其中任何一个移出网格,我需要程序结束。此刻,当一个变量关闭时,它会继续运行直到另一个变量运行。 (例如,如果它垂直移出网格,程序仍然会继续选择随机方向并水平移动。水平方向也是如此。)所以我不确定如何编写程序,以便当其中一个程序运行时结束关闭,而不是等待两者。这是我到目前为止所得到的:

import random
def rightmove(width, px):
px += 1
if px > width:
    return px
else:
    return px

def leftmove(width, px):
px -= 1
    if px == -1:
    return px
else:
    return px

def downmove(height, py):
py += 1
if py == height:
    return py
else:
    return py

def upmove(height, py):
py += 1
if py == -1:
    return py
else:
    return py

def main():
height = raw_input("Please enter the desired number of rows: ")
height = int(height)
width = raw_input("Please enter the desired number of columns: ")
width = int(width)
px = round(width/2)
px = int(px)
py = round(height/2)
py = int(py)
print "Manhattan (" + str(width) + ", " + str(height) + ")"
print "(x, y) " + str(px) + " " + str(py)
topy = height + 1
topx = width + 1
while 0 <= px <= width:
    while 0 <= py <= height:
    s = random.randint(0, 1)
    if s == 0:
        x = random.randint(0, 1)
        if x == 0:
            px = leftmove(width, px)
            if px <= 0:
                print "Direction E (x, y) " + str(px)
            else:
                print "Direction E"
        else:
            px = rightmove(height, px)
            if px <= width:
                print "Direction W (x, y) " + str(px)
            else:
                print "Direction W"
    else:
        y = random.randint(0, 1)
        if y == 0:
            py = downmove(height, py)
            if py <= height:
                print "Direction S (x, y) " + str(py)
            else:
                print "Direction S"
        else:
            py = upmove(height, py)
            if py <= 0:
                print "Direction N (x, y) " + str(py)
            else:
                print "Direction N"


main()

以下是预期输出的示例:

>>> manhattan(5,7)
(x, y) 2 3
direction N (x, y) 1 3
direction N (x, y) 0 3
direction S (x, y) 1 3
direction W (x, y) 1 2
direction S (x, y) 2 2
direction E (x, y) 2 3
direction S (x, y) 3 3
direction W (x, y) 3 2
direction N (x, y) 2 2
direction W (x, y) 2 1
direction E (x, y) 2 2
direction W (x, y) 2 1
direction N (x, y) 1 1
direction N (x, y) 0 1
direction S (x, y) 1 1
direction W (x, y) 1 0
direction E (x, y) 1 1
direction W (x, y) 1 0
direction W

2 个答案:

答案 0 :(得分:1)

虽然这只是猜测但没有看到你的实际缩进代码,我相信你的问题是:

while 0 <= px <= width:
    while 0 <= py <= height:
        # a whole mess of logic

如果py越界,你将逃脱内循环,但只是继续重复那个内循环,直到px也越界。

如果px越界,你仍然会陷入内循环,直到py超出范围。

一旦 超出范围,你想要的就是逃避。换句话说,您需要一个循环,只要两者都在边界内,它就会继续运行。您可以直接从英语翻译成Python:

while 0 <= px <= width and 0 <= py <= height:
    # same whole mess of logic

答案 1 :(得分:0)

我假设带有适当缩进的while循环看起来像这样:

while 0 <= px <= width:
    while 0 <= py <= height:
        s = random.randint(0, 1)
        if s == 0:
            x = random.randint(0, 1)
            if x == 0:
                px = leftmove(width, px)
                if px <= 0:
                    print "Direction E (x, y) " + str(px)
                else:
                    print "Direction E"
            else:
                px = rightmove(height, px)
                if px <= width:
                    print "Direction W (x, y) " + str(px)
                else:
                    print "Direction W"
        else:
            y = random.randint(0, 1)
            if y == 0:
                py = downmove(height, py)
                if py <= height:
                    print "Direction S (x, y) " + str(py)
                else:
                    print "Direction S"
            else:
                py = upmove(height, py)
                if py <= 0:
                    print "Direction N (x, y) " + str(py)
                else:
                    print "Direction N"

如果这是你所拥有的,那么解决方案很简单。首先,将if px <= 0:if py <= 0:更改为使用>=

然后,在每个意味着你完成的打印语句之后添加return语句。例如:

if px >= 0:
    print "Direction E (x, y) " + str(px)
else:
    print "Direction E"
    return

如果您使用return语句,则可以使用while而不是两个嵌套的while True:循环。

或者,如果您愿意,可以按照建议更改while循环的条件,而不用担心返回语句。