0

Here is my code which prints a particular number pattern. I want my number pattern to be in perfect triangular arrangement like:

a = int(input('Enter number: '))
base = a
while base > 0:
    for j in range(1, a + 1):
        print(' ' * (2 * j - 2), end = '')
        for i in range(1, base + 1):
            print(str(i), end = ' ')
        print()
        base -= 1

The output:

Enter number: 5
1 2 3 4 5 
  1 2 3 4 
    1 2 3 
      1 2 
        1
Enter number: 7
1 2 3 4 5 6 7 
  1 2 3 4 5 6 
    1 2 3 4 5 
      1 2 3 4 
        1 2 3 
          1 2 
            1 

The program works fine for numbers < 10 but when I input a number > 10 it gives a distorted pattern. For example:

Enter number: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 
    1 2 3 4 5 6 7 8 9 10 11 12 13 
      1 2 3 4 5 6 7 8 9 10 11 12 
        1 2 3 4 5 6 7 8 9 10 11 
          1 2 3 4 5 6 7 8 9 10 
            1 2 3 4 5 6 7 8 9 
              1 2 3 4 5 6 7 8 
                1 2 3 4 5 6 7 
                  1 2 3 4 5 6 
                    1 2 3 4 5 
                      1 2 3 4 
                        1 2 3 
                          1 2 
                            1

So is there a way to make the pattern right?

1
  • 1
    What is "right"? Please edit your question and add your expected output (just the first few rows would do) for that case. Commented Apr 29, 2020 at 8:51

3 Answers 3

2

If you want to have the same result for two digit numbers, you have to format your string. Here how it also works for two digit results:

a = int(input('Enter number: '))
base = a
while base > 0:
    for j in range(1, a + 1):
        print('  ' * (2 * j - 2), end = '')
        for i in range(1, base + 1):
            print('{0:>2}'.format(str(i)), end = '  ')
        print()
        base -= 1

Result for 15:

 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  
     1   2   3   4   5   6   7   8   9  10  11  12  13  14  
         1   2   3   4   5   6   7   8   9  10  11  12  13  
             1   2   3   4   5   6   7   8   9  10  11  12  
                 1   2   3   4   5   6   7   8   9  10  11  
                     1   2   3   4   5   6   7   8   9  10  
                         1   2   3   4   5   6   7   8   9  
                             1   2   3   4   5   6   7   8  
                                 1   2   3   4   5   6   7  
                                     1   2   3   4   5   6  
                                         1   2   3   4   5  
                                             1   2   3   4  
                                                 1   2   3  
                                                     1   2  
                                                         1 
Sign up to request clarification or add additional context in comments.

Comments

1

Some adjustments and str.rjust will do the trick:

a = base = 15
while base > 0:
    for j in range(a):
        print(' ' * 3 * j, end='')
        for i in range(base):
            print(str(i+1).rjust(3), end='')
        print()
        base -= 1

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
     1  2  3  4  5  6  7  8  9 10 11 12 13 14
        1  2  3  4  5  6  7  8  9 10 11 12 13
           1  2  3  4  5  6  7  8  9 10 11 12
              1  2  3  4  5  6  7  8  9 10 11
                 1  2  3  4  5  6  7  8  9 10
                    1  2  3  4  5  6  7  8  9
                       1  2  3  4  5  6  7  8
                          1  2  3  4  5  6  7
                             1  2  3  4  5  6
                                1  2  3  4  5
                                   1  2  3  4
                                      1  2  3
                                         1  2
                                            1

Comments

0

You might use rjust method of str, it does:

Return a right-justified string of length width. Padding is done using the specified fill character (default is a space).

Simple example usage:

numbers = [1, 10, 10, 1000, 10000]
for n in numbers:
    print(str(n).rjust(5))

Output:

    1
   10
   10
 1000
10000

Note that rjust requires at least one argument: width, if original str is shorther than width leading spaces (or other characters if specified) will be added to get str of length equal to width, otherwise original str will be returned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.