Python Forum

Full Version: Pyramid
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all,

As I am fairly new to python I am stuck with this.
I need to print pyramid like example below:

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
....


But my Pyramid is always starting from 1:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
...


My current code is:

def my_function(n):

    for i in range(1, n+1):

        for j in range(1,i):

            print(format(j, "4d"), end=' ')

        for i in range(i,0,-1):

            print(format(i, "4d"), end=' ')           

        print("")
        

 

def main():

    myNumber = int(input("Unesite broj do kojega zelite ici? "))

    print ("Vaš broj je: ", myNumber)

    type(myNumber)

    my_function(myNumber)

 

main();
What am I doing wrong and is there a way to make it with recursion?

Thank You all.

I got it, solution:

def my_function(n):

    for i in range(1, n+1):

        for j in range(i,i*2):

            print(format(j, "4d"), end=' ')

        for i in range(i*2,0,-1):

            print(format(i, "4d"), end=' ')           

        print("")
        

 

def main():

    myNumber = int(input("Unesite broj do kojega zelite ici? "))

    print ("Vaš broj je: ", myNumber)

    type(myNumber)

    my_function(myNumber)

 

main();
Hello, thank you for notifying and posting the solution to your issue.