class NumList: def __init__(self, col_num): self.col_num = col_num self.num_dict = {} self.num_list = [] def main(self): for i in range(1, self.col_num): # i为当前行数 print(' ' * (self.col_num - i), end=' ') # 定义左侧空白 k = (i * 2) - 1 # 每一行打印数量 for j in range(k): if i >= 2: if j == k - 2: # 定义每一行倒数第二的数值大小 print(i - 1, sep='', end=' ') self.num_list.append((i - 1)) # 记录显示的数值 continue elif j >= 1 and j < k - 2: # 第一行数值第一排 跟 倒数两排的数值 if i >= 3: print(self.num_dict[str(i - 1)][j - 1] + self.num_dict[str(i - 1)][j], sep='', end=' ') self.num_list.append(self.num_dict[str(i - 1)][j - 1] + self.num_dict[str(i - 1)][j]) continue print(i, sep='', end=' ') self.num_list.append(i) self.num_dict[str(i)] = self.num_list print() self.num_list = [] num = int(input("请输入显示行数:")) main = NumList(num) main.main()