编写一个 Python 程序,使用 for 循环打印左移数字的方阵。
rows = int(input("Enter Square of Left Shift Numbers Rows = "))
print("====The Square Pattern of Left Shift Numbers====")
for i in range(1, rows + 1):
for j in range(i, rows + 1):
print(j, end = ' ')
for k in range(1, i):
print(k, end = ' ')
print()

这是另一种编写 Python 程序以打印左移数字方阵的方法。
rows = int(input("Enter Square of Left Shift Numbers Rows = "))
print("====The Square Pattern of Left Shift Numbers====")
for i in range(1, rows + 1):
j = i
for k in range(1, rows + 1):
print(j, end = ' ')
j = j + 1
if j > rows:
j = 1
print()
Enter Square of Left Shift Numbers Rows = 8
====The Square Pattern of Left Shift Numbers====
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 1
3 4 5 6 7 8 1 2
4 5 6 7 8 1 2 3
5 6 7 8 1 2 3 4
6 7 8 1 2 3 4 5
7 8 1 2 3 4 5 6
8 1 2 3 4 5 6 7
这个 Python 示例 使用 while 循环从上到下显示左移数字的方阵。
rows = int(input("Enter Square of Left Shift Numbers Rows = "))
print("====The Square Pattern of Left Shift Numbers====")
i = 1
while(i <= rows):
j = i
while(j <= rows):
print(j, end = ' ')
j = j + 1
k = 1
while(k < i):
print(k, end = ' ')
k = k + 1
print()
i = i + 1
Enter Square of Left Shift Numbers Rows = 9
====The Square Pattern of Left Shift Numbers====
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8