Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyramid
#1
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();
Reply
#2
Hello, thank you for notifying and posting the solution to your issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print a * pyramid drogers10940 5 4,706 Apr-26-2020, 09:25 PM
Last Post: prothej227
  Pyramid of leters of n levels DanyMont1505 4 2,189 Sep-27-2019, 07:56 PM
Last Post: woooee
  Pyramid of Solitaires qwerty 1 3,461 Mar-22-2017, 04:10 PM
Last Post: nilamo
  Implementation Pyramid Solitaire qwerty 2 5,066 Feb-27-2017, 11:57 AM
Last Post: qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020