4

I am not getting how to do it, I keep on running into a problem.

The following is the code:

rows = int(input())

for i in range(1,rows):
    for j in range(1,i+1):
        print(" ", end='')

    for j in range(i, rows+1):
        print(j, end='')
    print()

for i in range(rows,0,-1):
    for j in range(1,i+1):
        print(" ", end='')

    for i in range(i, rows+1):
        print(j,end='')
        j = j+1   
    print()
**My Output**
 12345
  2345
   345
    45
     5
    45
   345
  2345
 12345
**Expected Output**
12345
 2345
  345
   45
    5
   45
  345
 2345
12345

The space in the first column.

How to remove it ????

here, rows = 6 (as input from the user)

3
  • 2
    How about you shorten your space-printing loops by one iteration? Commented Jun 13, 2020 at 21:12
  • I did and then it messed up more. Commented Jun 13, 2020 at 21:17
  • In practice, you'd just construct the string of digits, then use the rjust method to pad the string with spaces to the desired width. Commented Jun 16, 2024 at 14:43

4 Answers 4

3

I think this is somewhat cleaner:

lst = [str(i) for i in range(1, rows+1)]
for i in range(rows):
    print((" "*i) + "".join(lst[i:]) )

for i in range(rows-2, -1, -1):
    print((" "*i) + "".join(lst[i:]) )

which results in:

12345
 2345
  345
   45
    5
   45
  345
 2345
12345
Sign up to request clarification or add additional context in comments.

Comments

1

This:

for i in range(1,rows):
    for j in range(1,i+1):  # prints at least one spacce
        print(" ", end='')

given any positive row number (f.e. rows=6) always starts with printing at least one space.

You also mangled the lower number printing loop and printed j while counting i.

Fix:

rows = int(input())

for i in range(1,rows+1):
    for j in range(1,i):      # fix here
        print(" ", end='')

    for k in range(i, rows+1):
        print(k, end='')
    print()

for i in range(rows-1,0,-1):
    for j in range(1,i):      # fix here 
        print(" ", end='')

    for i in range(i, rows+1):   # some fixing here,
        print(i,end='')          # you mangled i/j
    print()

For this kind of formatting the string format mini language can be used:

rows = int(input())   # 5
tobeprinted=''.join(map(str,range(1,rows+1)))

for n in range(rows):
    print(f"{tobeprinted[n:]:>{rows}}")   # right align formatting
for n in range(rows-2 ,-1,-1):
    print(f"{tobeprinted[n:]:>{rows}}")   # right align formatting

Output:

12345
 2345
  345
   45
    5
   45
  345
 2345
12345

Comments

0

Change the iterating variable to j from i and to avoid the extra spaces try to decrease the range by one

rows = 5

for i in range(1,rows):
    for j in range(1,i):       #made changes here
        print(" ", end='')
    for j in range(i, rows+1):
        print(j, end='')
    print()

for i in range(rows,0,-1):
    for j in range(1,i):       #made changes here
        print(" ", end='')
    for j in range(i, rows+1): #made changes here
        print(j,end='')
        j = j+1   
    print()

2 Comments

Please, edit and try for How to Answer, describe the effect of what you propose and explain why it helps to solve the problem, make the additional insight more obvious which you contribute beyond existing answers, the relevant advantage which your solution achieves, especially if the difference is so small and non-obvious. Consider taking the tour.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

Made some changes to code (range start from 2). It works now. However, I believe your code is not the optimal way to approach this.


    rows = int(input())

    for i in range(1,rows):
        for j in range(2,i+1):
            print(" ", end='')
    for j in range(i, rows+1):
        print(j, end='')
    print()

    for i in range(rows,0,-1):
        for j in range(2,i+1):
            print(" ", end='')
    for j in range(i, rows+1):
        print(j,end='')
        j = j+1   
    print()

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.