编写一个 Python 程序,使用 for 循环打印除对角线数字图案外其余部分均为零的方阵。
rows = int(input("Enter Square With Diagonal Numbers Rows = "))
print("====The Square With Diagonal Numbers and Remaining 0's Pattern====")
for i in range(1, rows + 1):
for j in range(1, i):
print('0', end = ' ')
print(i, end = ' ')
for k in range(i, rows):
print('0', end = ' ')
print()

这是在 Python 中打印带有对角线数字以及其余零的方阵图案的另一种方法。
rows = int(input("Enter Square With Diagonal Numbers Rows = "))
print("====The Square With Diagonal Numbers and Remaining 0's Pattern====")
for i in range(1, rows + 1):
for j in range(1, rows + 1):
if i == j:
print(i, end = ' ')
else:
print('0', end = ' ')
print()
Enter Square With Diagonal Numbers Rows = 8
====The Square With Diagonal Numbers and Remaining 0's Pattern====
1 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0
0 0 3 0 0 0 0 0
0 0 0 4 0 0 0 0
0 0 0 0 5 0 0 0
0 0 0 0 0 6 0 0
0 0 0 0 0 0 7 0
0 0 0 0 0 0 0 8
这个 Python 程序使用 while 循环显示了递增对角线数字的方阵图案。其余部分均为零。
rows = int(input("Enter Square With Diagonal Numbers Rows = "))
print("====The Square With Diagonal Numbers and Remaining 0's Pattern====")
i = 1
while(i <= rows):
j = 1
while(j <= rows):
if i == j:
print(i, end = ' ')
else:
print('0', end = ' ')
j = j + 1
print()
i = i + 1
Enter Square With Diagonal Numbers Rows = 9
====The Square With Diagonal Numbers and Remaining 0's Pattern====
1 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0
0 0 0 0 5 0 0 0 0
0 0 0 0 0 6 0 0 0
0 0 0 0 0 0 7 0 0
0 0 0 0 0 0 0 8 0
0 0 0 0 0 0 0 0 9