# 带环的 python 太空塔

[英]space tower in python with loops

``````1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8     8 7 6 5 4 3 2 1
1 2 3 4 5 6 7         7 6 5 4 3 2 1
1 2 3 4 5 6             6 5 4 3 2 1
1 2 3 4 5                 5 4 3 2 1
1 2 3 4                     4 3 2 1
1 2 3                         3 2 1
1 2                             2 1
1                                 1
``````

``````number = int(input("enter a number to create your triangle: "))

for col in range(number,0,-1):
for row in range(1,col):
print(row,  end=" ")

if col<number:
print(" "*(row*2), end="")

for row in range(col-1,0,-1):
print(row, end=" ")
print()
``````

``````number = 9

base = [str(n) for n in range(1, number+1)]
for i, idx in enumerate(range(len(base)-1, 0, -1)):
if i == 0:
print(' '.join(base + base[::-1]))
base[idx] = ' '
print(' '.join(base + base[::-1]))
``````

``````1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8     8 7 6 5 4 3 2 1
1 2 3 4 5 6 7         7 6 5 4 3 2 1
1 2 3 4 5 6             6 5 4 3 2 1
1 2 3 4 5                 5 4 3 2 1
1 2 3 4                     4 3 2 1
1 2 3                         3 2 1
1 2                             2 1
1                                 1
``````

``````number = int(input("enter a number to create your triangle: "))

for i, n in enumerate(range(number, 0, -1)):
print(*range(1, n + 1), *(" " * (i * 2)), *range(n, 0, -1))
``````

Output：

``````1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8     8 7 6 5 4 3 2 1
1 2 3 4 5 6 7         7 6 5 4 3 2 1
1 2 3 4 5 6             6 5 4 3 2 1
1 2 3 4 5                 5 4 3 2 1
1 2 3 4                     4 3 2 1
1 2 3                         3 2 1
1 2                             2 1
1                                 1
``````