Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a pyramid using recursion in python?

I need to make a pyramid in python using recursion. Already made it, but I need help making it with recursion.

def pyramid(n):
    for i in range(0, n):

        for j in range(0, i+1):
    
            print("* ",end="")
      
        print("\r")
 
pyramid(5)
like image 214
Alitaliano Avatar asked Jan 24 '26 23:01

Alitaliano


1 Answers

Recursion = the repeated application of a recursive procedure.

Code:

def pyramid(n):
    if n==0:
        return
    else:
        pyramid(n-1)
        print("* "*n)

n = 10
pyramid(n)

This just repeats the function until n = 0.

like image 192
Matei Piele Avatar answered Jan 26 '26 13:01

Matei Piele