我的代码未打印为以下示例

时间:2018-11-24 08:59:11

标签: python python-3.x

grid = []
 for row in range(4):
        grid.append([])
    for column in range(3):
        grid[row].append([0])
    for row in range(4):
        print('|', end =' ')
        for column in range(3):
            print(grid[row][column])
            if(column == 3):
                print('|')

它没有按我期望的那样打印下面的框

|         |
|         |
|         |
|         |

2 个答案:

答案 0 :(得分:1)

您的代码有点混乱。看起来您想创建一个由行和列组成的网格,并且网格的内容应类似于您描述的输出。

我对您的代码进行了一些调整,并添加了一些说明。看看这是否适合您。

#first, lets create the grid.
grid = []
for row in range(4):
    grid.append([])
    for column in range(3):
        if column in [0, 2]: #if it's the first or the last column, add a "|"
            grid[row].append('|')
        else: #if it's any other columnt, add a space " "
            grid[row].append(' ')

#now that we filled the grid, let's iterate over it and print each entry.
for row in grid: 
    for entry in row:
        print(entry,end=' ')
    print() #print a newline after each row

答案 1 :(得分:-1)

您将其视为复杂问题,这是一个简单的解决方案:

for i in range(4):
  print('|' + (' '*9 +'|'))

最适合您的

相关问题