杨辉三角python小代码

刚刚上机课,老师居然让我们打印杨辉三角??

        • 结果

def yangHuiSanJiao(n):
    
    #打印第一行和第二行
    print("1".center(50))
    next_line = [1,1]
    
    second_line = ' '.join([str(i) for i in next_line])
    print(second_line.center(50))

    for i in range(2,n):
        mid = []
        
        for i in range(0, len(next_line)-1):
            mid.append(next_line[i] + next_line[i+1])
            
        next_line = [1] + mid + [1]
        fin = ' '.join([str(i) for i in next_line])
        print(fin.center(50))
        
yangHuiSanJiao(5)

结果

杨辉三角python小代码_第1张图片

你可能感兴趣的:(Python模块笔记,python,杨辉三角)