1
     ************
      **********
       ********
        ******
         ****
          **

code for the above:

j=0
k=12
for i in range(6):
    print(' '*(j),end=' ')
    print('*'*k,end=' ')
    j+=1
    k-=2
    print()

But I need to print the above pyramid using recursion and make sure both the spaces and asterisks are aligned as shown.

1 Answers1

0

This recursive function prints the same shape as your example :

width = 12

def printRow(i):
    print(' ' * i, end=' ')
    print('*' * (width - i * 2), end=' ')
    print()
    if (i < width // 2): printRow(i+1)

printRow(0)
dspr
  • 2,383
  • 2
  • 15
  • 19