在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
答案 0 :(得分:1)
下面的代码应该这样做:-
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()
答案 1 :(得分:0)
问题是,您已将列起始为4,为什么还要打印“列5”。是您想要的东西还是您尝试传达的逻辑有问题?