How to print a Pythagorean pyramid in python?

I wrote this program to print the Pythagorean pyramid with its triangular shape. It can print it up to a certain point (before two-digit numbers appear). After that, the shape is distorted. How can I fix this?

l=[[1],[1,1]]
n=int(raw_input("Number:"))
for x in range(2,n):
    l2=[1,]
    for y in range(x-1):
        l2.append(l[x-1][y]+l[x-1][y+1])
    l2.append(1)
    l.append(l2)
for x in range(n):
    print " "*(n-x-1),
    for y in l[x]:
        print y,
    print 

      

+3


source to share


1 answer


You will need to fill your output with spaces. Note that the number of spaces is a function of the longest length you will be printing; if you click on one place, your result will be wrong again as soon as you start typing three-digit numbers.



0


source







All Articles