Python:使用两个while循环创建网格。不适用于循环

2024-04-25 04:12:02 发布

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

我很难让while循环保持循环,直到满足这两个条件。我只应该使用while循环或者if语句/布尔值(如果需要)。

我想打印一个游戏的网格大小长X宽。 所以假设

LENGTH = 5
WIDTH = 6

def create_grid(grid):

    x = 0
    y = 0

    while x < WIDTH and y < LENGTH:
          table = print(grid[WIDTH * x + y] + '|')
          x +=1
          y +=1

    return table

如何使上述表达式的x+=1和y+=1的while循环到循环? 我希望它能打印出这个表达式

while y < LENGTH:
    print('|' + grid[(0) * WIDTH + y] + '|' + '|' + grid[(1) * WIDTH + y] + ...
    + '|' + grid[(all the way up to WIDTH - 1) * WIDTH + y] + '|')

    y += 1

return ###expression above

我放在括号里,我想用数字来代替x,x+1,…一直到x=WIDTH-1(或x<;WIDTH)。

我想我可以将这些片段连接起来,只使用一个while循环,但是如果网格尺寸很大,那么写出整个网格将占用太多空间。


Tags: 网格游戏returnif表达式defcreatetable
3条回答

从概念上讲,while循环与for循环完全相同,只是没有增量。因此,如果需要使用while实现循环,只需按照for循环的方式执行,但要自己递增。所以这意味着你只需要嵌套两个whiles:

x = 0
table = ''

while x < WIDTH:
    y = 0
    while y < LENGTH:
        table += grid[WIDTH * x + y] + '|'
        y +=1
    table += '\n'
    x +=1

return table

作为示例给出的两个示例代码的问题是,您只使用了一个循环。

一个可能有助于解决(编程)问题的“诀窍”是试图描述你将如何“用手”做这件事。在这里,要绘制网格,应该从第一列的第1行绘制到第H行的单元格,然后对第2列再次绘制,依此类推。

在pseuso代码中:

HEIGHT = 5
WIDTH = 6

current_row = 0
current_column = 0

# Draw the first column
while current_row < HEIGHT:
    draw_cell(current_column, current_row)
    current_row += 1

# Draw next column column
current_column += 1
while current_row < HEIGHT:
    draw_cell(current_column, current_row)
    current_row += 1

# ...
# ... and so on until last column
current_column += 1
while current_row < HEIGHT:
    draw_cell(current_column, current_row)
    current_row += 1

显然,有很多重复的相同的代码。这需要嵌套循环。这是你在另一个循环中的第一个循环:

HEIGHT = 5
WIDTH = 6

current_row = 0
current_column = 0

# For each column
while current_column < WIDTH:
    # Draw the column
    while current_row < HEIGHT:
        draw_cell(current_column, current_row)
        current_row += 1

    # The "inner" loop is done.
    # Time to go to the next column
    current_column += 1

这里我使用了一个while循环,因为它似乎是您喜欢的。但正如其他人已经说过的,这种事情的“自然”工具是for循环。

通常情况下,您可以使用一个for循环来完成此操作;我只使用切片:

def create_grid(grid):
    table = ''

    for y in range(0, LENGTH * WIDTH, WIDTH):
        table += '|'.join(grid[y:y+WIDTH]) + '\n'

    return table

对于一个while循环,在这个循环中,您可以使用:

def create_grid(grid):
    y = 0
    size = LENGTH * WIDTH
    table = ''
    while y < size:
        table += '|'.join(grid[y:y+WIDTH]) + '\n'
        y += WIDTH

    return table

注意,我们只是一次跳过整个网格的步骤,让每行的步骤都工作。你也可以在循环中完成这一部分:

def create_grid(grid):
    y = 0
    size = LENGTH * WIDTH
    table = ''
    while y < size:
        x = 0
        while x < WIDTH:
            if x:
                table += '|'
            table += grid[y + x]
            x += 1
        table += '\n'
        y += WIDTH

    return table

要按列列出网格(因此,换位),需要使用:

def create_grid(grid):
    y = 0
    size = LENGTH * WIDTH
    table = ''
    while y < LENGTH:
        x = 0
        while x < size:
            if x:
                table += '|'
            table += grid[y + x]
            x += LENGTH
        table += '\n'
        y += 1

    return table

相关问题 更多 >