在python 3中打印数字金字塔

时间:2018-10-23 07:23:24

标签: python python-3.x algorithm

Okey,实际上我真的不知道如何在python中打印金字塔,更糟糕的是,我需要得到这个: Pyramid of n numbers

询问金字塔的长度

这是代码。

n = int(input("\n Enter the lenght of the triangle.. "))

for i in range(n):
    for j in range(n - i):
        print(" ", end = " ")
    for k in range(i):
        print("*", end = " ")
    print()

Here is the output

1 个答案:

答案 0 :(得分:1)

选项之一:

import numpy as np
a = int(input())
def pyram(a):
    n = np.zeros(a)
    hal = int((a+2)/2)+1
    n[:(hal-1)] = range(1,hal)
    n[(hal-1):] = np.array(range(1,hal-1))[::-1]
    print(n)
for i in range(1,a*2,2):
    pyram(i)