编写一个Python程序,使用for循环打印正方形图案的四边相同的数字。
rows = int(input("Enter Square of same Numbers Pattern Rows = "))
print("====Print Same Numbers on all Sides of a Square Pattern====")
for i in range(1, rows + 1):
for j in range(1, rows + 1):
if i < j:
print(rows - i + 1, end = ' ')
else:
print(rows - j + 1, end = ' ')
for k in range(rows - 1, 0, - 1):
if i < k:
print(rows - i + 1, end = ' ')
else:
print(rows - k + 1, end = ' ')
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows + 1):
if i < j:
print(rows - i + 1, end = ' ')
else:
print(rows - j + 1, end = ' ')
for k in range(rows - 1, 0, - 1):
if i < k:
print(rows - i + 1, end = ' ')
else:
print(rows - k + 1, end = ' ')
print()

这个Python示例使用while循环打印出四边具有相同数字的正方形图案。
rows = int(input("Enter Square of same Numbers Pattern Rows = "))
print("====Print Same Numbers on all Sides of a Square Pattern====")
i = 1
while(i <= rows):
j = 1
while(j <= rows):
if i < j:
print(rows - i + 1, end = ' ')
else:
print(rows - j + 1, end = ' ')
j = j + 1
k = rows - 1
while(k >= 1):
if i < k:
print(rows - i + 1, end = ' ')
else:
print(rows - k + 1, end = ' ')
k = k - 1
print()
i = i + 1
i = rows - 1
while(i >= 1):
j = 1
while(j <= rows):
if i < j:
print(rows - i + 1, end = ' ')
else:
print(rows - j + 1, end = ' ')
j = j + 1
k = rows - 1
while(k >= 1):
if i < k:
print(rows - i + 1, end = ' ')
else:
print(rows - k + 1, end = ' ')
k = k - 1
print()
i = i - 1
Enter Square of same Numbers Pattern Rows = 9
====Print Same Numbers on all Sides of a Square Pattern====
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9
9 8 7 7 7 7 7 7 7 7 7 7 7 7 7 8 9
9 8 7 6 6 6 6 6 6 6 6 6 6 6 7 8 9
9 8 7 6 5 5 5 5 5 5 5 5 5 6 7 8 9
9 8 7 6 5 4 4 4 4 4 4 4 5 6 7 8 9
9 8 7 6 5 4 3 3 3 3 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 2 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 2 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 3 3 3 3 4 5 6 7 8 9
9 8 7 6 5 4 4 4 4 4 4 4 5 6 7 8 9
9 8 7 6 5 5 5 5 5 5 5 5 5 6 7 8 9
9 8 7 6 6 6 6 6 6 6 6 6 6 6 7 8 9
9 8 7 7 7 7 7 7 7 7 7 7 7 7 7 8 9
9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9