你很接近,你可以
str.center
每行:
inc = int(input('Input number of rows: '))
n = 0
row = []
while n < inc:
m = 11 ** n
row.append(m)
n += 1
for i in range(0, len(row)):
row[i] = ' '.join(list(str(row[i])))
for i in range(0, len(row)):
row[i] = row[i].center(len(row[-1]),' ')
result = '\n'.join(row)
print(result)
输出:
Input number of rows: 4
1
1 1
1 2 1
1 3 3 1
较短的版本是:
inc = int(input('Input number of rows: '))
max_len = 2 * len(str(11**inc)) - 1
row = (' '.join([*str(11**p)]).center(max_len,' ') for p in range(inc))
print(*row, sep='\n')
输出:
输入行数:4
1个
11个
1 2 1号
1 3 3 1