1

I want to print this pattern:

         1
        121
       12321
      1234321
     123454321
    12345654321
   1234567654321
  123456787654321
 12345678987654321
12345678910987654321

My program for this:

n=11
for i in range(1,n):
    for j in range(1,n-i):
        print(end=' ')
    for j in range(1,i+1):
        print(j,end='')
    for j in range(1,i-1):
        print(j,end='')
    print()

But this program is not printing the desired pattern. Instead it is printing:

         1
        12
       1231
      123412
     12345123
    1234561234
   123456712345
  12345678123456
 1234567891234567
1234567891012345678

Where am I going wrong? Please do help.Thanks in advance.

  • "But this program is not printing the desired pattern"—what _is_ it printing? Please read [ask]. – ChrisGPT was on strike Apr 21 '20 at 16:03
  • 1
    In your final `for` loop. To reverse the order `range` iterates, you'll need to use the 3rd "step" parameter for `range`. The arguments for `range` are `start` where it should start, `stop` where the iteration should stop, and `step`, the amount you should iterate on each step. Since you count up to `i` on your previous for loop, you'll probably want to start at 1 less than that. I'll let you figure out where to stop and the amount to step by. – Axe319 Apr 21 '20 at 16:11
  • 1
    Thanks got it:). I changed the last for loop to ```for j in range(i-1,0,-1):``` – Shreya Reddy Apr 21 '20 at 16:20
  • 1
    Change the 2nd for loop as for j in range(n,i,-1): – jose_bacoy Apr 21 '20 at 17:09

0 Answers0