def fun(n, line, inx, flag=1):
if line == n + 1 or line == 0: return inx
for ii in range(n - line):
print(end=' ')
for i in range(line):
print(inx % 10, end=' ')
inx += 1
print()
line += flag
t = fun(n, line, inx,flag)
return t
n = 6
t = fun(n, 1, 0)
fun(n, n-1, t,-1)
1.生成一个初始值全为-1的二维数组
2.将需要填写数字的位置全部赋值为-2
3.按从左到右、从上到下的顺序将值为-2的位置依次赋值为0-9循环
4.按从左到右、从上到下的顺序输出所有值在0-9范围内的值
def draw(currentDepth,height,num,middel = None):
'''
height: 表示绘制的总高度,需为奇数
currentDepth:表示当前高度
num: 表示本层开始的数字
middle: 表示中间的层数
'''
if not middel:
# 中间层数未知
middel = height//2 +1
currentDepth += 1
# 获取当前层前至空格数
spaceNum = abs(currentDepth - middel)
# 获取当前层数字的数量
numNum = abs(middel - spaceNum)
res = " "*spaceNum
endNum= num + numNum
for i in range(num,endNum,1):
res += str(i%10)+" "
print(res)
if currentDepth != height:
# 不是最后一层,递归调用
draw(currentDepth,height,endNum,middel)
draw(0,13,0)
试试这段代码是否符合你的要求不?