Shreya Reddy
Shreya Reddy

Reputation: 99

How to print the below pattern in python?

I want to print this pattern

         1
        12A
       123BA
      1234CBA
     12345DCBA
    123456EDCBA
   1234567FEDCBA
  12345678GFEDCBA
 123456789HGFEDCBA
12345678910IHGFEDCBA

My code for this pattern:

n=11
a=65
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(i-1,0,-1):
        ch=chr(a)
        print(ch,end='')
        a=a+1
    print()

But this is printing:

         1
        12A
       123BC
      1234DEF
     12345GHIJ
    123456KLMNO
   1234567PQRSTU
  12345678VWXYZ[\
 123456789]^_`abcd
12345678910efghijklm

I think the problem lies in the last for loop but I am unable to rectify it. Can someone please help? Thanks in advance.

Upvotes: 0

Views: 202

Answers (5)

Shehan
Shehan

Reputation: 186

Here is a small improvement.

n=11
a=65
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(i-2,-1,-1):
        if i != 1:
            ch=chr(a+j)
            print(ch,end='')
    print()

Upvotes: 3

Surprised
Surprised

Reputation: 21

well I like this:

letters="IHGFEDCBA"
lines = 11
for i in range(lines,-1,-1):
    print(i*' ' + ''.join([str(num) for num in range(1, lines-i)]) + letters[i:])

but I am not confident that the list comprehension is worth it.

Upvotes: 1

fireball.1
fireball.1

Reputation: 1521

You were not resetting the value of a at every iteration of i and not using the value of j in the last loop.

n=11

for i in range(1,n):
    a=64  #note this is inside loop now and the value was reduced by 1
    for j in range(1,n-i):
        print(end=' ')
    for j in range(1,i+1):
        print(j,end='')
    for j in range(i-1,0,-1):
        ch=chr(a+j)  #note the change here
        print(ch,end='')
        a=a+1
    print()

Since your a was never being reset to 65, the value shot past 90 and started printing the corresponding ascii characters.

Upvotes: 1

RomainL.
RomainL.

Reputation: 1014

I you were really close:

n=11
a=65
for i in range(1,n):
    a=65+i-2 # a need to actually end on char A
    for j in range(1,n-i):
        print(end=' ')
    for j in range(1,i+1):
        print(j,end='')
    for j in range(i-1,0,-1):
        ch=chr(a)
        print(ch,end='')
        a=a-1 # here we substract so we go backward!
    print()

Upvotes: 1

FlyingTeller
FlyingTeller

Reputation: 20472

After the first line is printed a has changed from value 65, which is why you just get increasing letters until you are at character codes for special characters. Keep it fixed and use the loop variable to claculate the correct letter:

n=11
a = 65
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(i-1,0,-1):
        ch=chr(a+j-1)    # Note the change here
        print(ch,end='')

    print()

This prints

         1
        12A
       123BA
      1234CBA
     12345DCBA
    123456EDCBA
   1234567FEDCBA
  12345678GFEDCBA
 123456789HGFEDCBA
12345678910IHGFEDCBA

Upvotes: 3

Related Questions