How to do print formatting in Python with chunks o

2019-09-09 22:51发布

问题:

I'm having some trouble with formatting the pyramid. I've tried to use format when printing from the loop but that didn't seem to work and just breaks the program. What would be different ways to format the output. The only trouble that I am having is when I am printing 10 and up when there's double digits. What would be the best approach formatting the printing output? I've tried variety of ways but couldn't make formatting work within the loop from documentation https://docs.python.org/3.5/library/string.html#formatstrings

Here is the script:

userinput = int(input("Enter the number of lines: " ))   # User input of the total number of lines
userinput = userinput + 1   # adding a value of 1 additionally with the user input to make numbers even

for i in range(1, userinput):   # Loop through lines from 1 to userinput

    for j in range(userinput - i):  # printing spaces, 1 at a time from j = 1 to j = userinput - i 
        print(" ", end = " ")

    for j in range(i, 0, -1):  # printing number decreasing from the line number j to 1
        print(j, end = " ")

    for j in range(2,i + 1):   # Printing number increasing from 2 to line number j
        print(j, end = " ")
    print()
    j += 1  

The output when its less than 10

Enter the number of lines: 9
                  1 
                2 1 2 
              3 2 1 2 3 
            4 3 2 1 2 3 4 
          5 4 3 2 1 2 3 4 5 
        6 5 4 3 2 1 2 3 4 5 6 
      7 6 5 4 3 2 1 2 3 4 5 6 7 
    8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 
  9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 

The output when it's 15 or more:

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

When I have reserved an extra space for 10 and up, here is what my outout looks like: (The dots were used to distinguish from empty space, all I did was added a " " quotes in the beginning of the print.

Enter the number of lines: 12
. . . . . . . . . . . .   1                        
. . . . . . . . . . .   2  1  2                      
. . . . . . . . . .   3  2  1  2  3                    
. . . . . . . . .   4  3  2  1  2  3  4                  
. . . . . . . .   5  4  3  2  1  2  3  4  5                
. . . . . . .   6  5  4  3  2  1  2  3  4  5  6              
. . . . . .   7  6  5  4  3  2  1  2  3  4  5  6  7            
. . . . .   8  7  6  5  4  3  2  1  2  3  4  5  6  7  8          
. . . .   9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9        
. . .   10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10      
. .   11  10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10  11    
.   12  11  10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10  11  12  

Here is what I've tried changing by adding aditional space

  for j in range(userinput - i):  # printing spaces, 1 at a time from j = 1 to j = userinput - i 
        print(".", end = " ")

    for j in range(i, 0, -1):  # printing number decreasing from the line number j to 1
        print(" ", j, end = "")

    for j in range(2,i + 1):   # Printing number increasing from 2 to line number j
        print(" ", j, end = "")

    for j in range(userinput - i):  # printing spaces, 1 at a time from j = 1 to j = userinput - i 
        print(" ", end = " ")

Here is the ideal output of what I am trying to accomplish:

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

Thank you!

回答1:

The things to consider for this problem are

  • The length of the largest number.
  • The length of the current number being printed.
  • The difference in lengths.

In order to correctly space everything, you're going to need to print extra spaces after the numbers with less digits (to compensate for the extra digits in the larger number).

For example, if you have a row that contains the number 10, in order to correctly space the other smaller numbers, you're going to need to use extra spaces to compensate for that second digit in the number 10.

This solution works for me.

userinput = int(input("Enter the number of lines: " ))
userinput = userinput + 1   

# Here, you can see I am storing the length of the largest number
input_length = len(str(userinput))

for i in range(1, userinput):

    # First the row is positioned as needed with the correct number of spaces
    spaces = " " * input_length
    for j in range(userinput - i): 
        print(spaces, end = " ")

    for j in range(i, 0, -1):
        # Now, the current numbers length is compared to the 
        # largest number's length, and the appropriate number
        # of spaces are appended after the number.
        spaces = " " * (input_length + 1 - len(str(j)))
        print(j, end = spaces)

    for j in range(2,i + 1):
        # The same is done here as in the previous loop.
        spaces = " " * (input_length + 1 - len(str(j)))
        print(j, end = spaces)
    print()
    j += 1  


回答2:

Take a look at https://stackoverflow.com/a/13077777/6510412

I think this might be what you're looking for. I hope it helps.