使用嵌套循环在Python上创建表

使用嵌套循环在Python上创建表,python,python-3.x,Python,Python 3.x,在python上创建表时,我遇到了一个问题。代码如下: row = 5 col = 4 for x in range(1, row + 1): print("Row", x, end="") for y in range(1, col + 1): print(" Column", y, end="") print(" Row", x, end="") print() 这就是运行的内容: Row 1 Column 1 Row 1 Colum

在python上创建表时,我遇到了一个问题。代码如下:

row = 5
col = 4
for x in range(1, row + 1):
    print("Row", x, end="")
    for y in range(1, col + 1):
        print(" Column", y, end="")
        print(" Row", x, end="")
    print()
这就是运行的内容:

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5
如您所见,它以行结尾,缺少第5列。我能做些什么来解决这个问题

这是我期望的输出:

Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4 Row 1 Column 5
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4 Row 2 Column 5
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4 Row 3 Column 5
Row 4 Column 1 Row 4 Column 2 Row 4 Column 3 Row 4 Column 4 Row 4 Column 5
Row 5 Column 1 Row 5 Column 2 Row 5 Column 3 Row 5 Column 4 Row 5 Column 5

应使用以下代码执行此操作:-

row = 5
col = 5
for x in range(1, row + 1):
    for y in range(1, col + 1):
        print(" Row", x, end="")
        print(" Column", y, end="")
    print()

问题是,您已经启动了列as 4,为什么您希望它打印“列5”。这是您想要的还是您试图传达的逻辑有问题?

您希望得到什么输出?@Sumit刚刚编辑了我想要的输出