如何使用大块字符串在Python中打印格式?

时间:2016-07-23 01:46:10

标签: loops python-3.x for-loop formatting string-formatting

我在格式化金字塔方面遇到了一些麻烦。我从环路打印时尝试使用格式,但这似乎不起作用,只是打破了程序。什么是格式化输出的不同方法。我遇到的唯一麻烦就是当我打印10和以上时有两位数。格式化打印输出的最佳方法是什么?我尝试了各种方法,但无法在文档循环中进行格式化工作 https://docs.python.org/3.5/library/string.html#formatstrings

这是脚本:

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  

小于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 

15岁或以上时的输出:

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 

当我为10岁及以上预留了额外的空间时,这就是我的外出的样子:(这些点用于区分空白空间,我所做的只是在开头的时候添加了" "引号印刷品。

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  

以下是我尝试通过添加附加空间进行更改

  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 = " ")

这是我想要完成的理想输出:

                                           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 

谢谢!

2 个答案:

答案 0 :(得分:1)

看一看 https://stackoverflow.com/a/13077777/6510412

我认为这可能是你正在寻找的东西。我希望它有所帮助。

答案 1 :(得分:1)

此问题需要考虑的事项是

  • 最大数字的长度。
  • 正在打印的当前号码的长度。
  • 长度的差异。

为了正确地划分所有内容,您需要额外打印 数字少于数字后的空格(以补偿较大数字中的额外数字)。

例如,如果您有一个包含数字10的行,为了正确地隔开其他较小的数字,您将需要使用额外的空格来补偿数字10中的第二个数字。

此解决方案适合我。

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