-1

I want to produce a similar shape to the above using a nested loop. Can anyone here help me with that?

print('        #')
print('       ###')
print('      ####')
print('     #####')
print('    #######')
print('   #########')
print('  ###########')
print(' ############')
print('###############')


for x in range(0, 10):
    print(' #')
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 1
    @PatrickArtner Where are you seeing flag on a pole shape? If I go to the revision history and click the "edit" link on OP's original version, this is what I see: https://i.stack.imgur.com/jeNmA.png The only spacing/layout change I can see is the one you made. – TylerH Jun 18 '21 at 13:52
  • Does this answer your question? [how-to-print-a-pyramid-in-python](https://stackoverflow.com/questions/61578101/how-to-print-a-pyramid-in-python) – Patrick Artner Jun 18 '21 at 13:52
  • @Tyler Original post by the autor: http://stackoverflow.com/revisions/68035830/1 - all # are prefixed by 1 space in the `print(...)` statements ? – Patrick Artner Jun 18 '21 at 13:55
  • 2
    @PatrickArtner The link you are sharing does not have any markdown view visible. Go to the [revisions page](https://stackoverflow.com/posts/68035830/revisions), scroll to the bottom, and click "edit" on the original version by OP to see their intended markdown. Multiple spaces in non-code are ignored by the browser when rendering, so in non-code it *looks* like one space. – TylerH Jun 18 '21 at 13:56
  • Ouch sorry for my confusion then – Patrick Artner Jun 18 '21 at 13:57

1 Answers1

2

Here is the code for you.

Code for # program using nested loop

def triangle(n):
    k = n - 1
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i+1):
            print("# ", end="")
        print("\r")
n = 10
triangle(n)

For each line-number (i-th line) we need to print n-(i+1) spaces followed by i+1 #. Our for i in range(0,n): delivers i from 0 to n-1. k is used to easier keep track of needed spaces.

For 10 the output is:

         #
        # #
       # # #
      # # # #
     # # # # #
    # # # # # #
   # # # # # # #
  # # # # # # # #
 # # # # # # # # #
# # # # # # # # # #
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Vijayaragavan K
  • 319
  • 1
  • 5