2016-07-23 53 views
0

我在格式化金字塔時遇到了一些麻煩。從循環打印時,我試圖使用格式,但似乎沒有工作,只是打破了程序。什麼是不同的方式來格式化輸出。我遇到的唯一的麻煩是當我打印10以上時,有兩位數字。格式化打印輸出的最佳方法是什麼?我已經試過各種方法,但不能從文檔 https://docs.python.org/3.5/library/string.html#formatstrings如何使用大塊字符串在Python中打印格式?

這裏讓內環路格式的工作是腳本:

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個或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 

這裏就是我試圖改變通過添加aditional的空間

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 

謝謝!

回答

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 
+0

謝謝!如果你有時間添加更多的解釋,那將是非常好的,因爲這正是我想要了解的更多。我會看看嘗試和理解。 – Dmitriy

+0

@Dmitriy,我已經用解釋更新了我的答案。我希望它有幫助。 – Paradoxinabox

+0

謝謝你的解釋。我想了解的一件事是'spaces =「」*(input_length + 1 - len(str(j)))''所以如果我理解正確,那麼當兩個數字,所以它做的是''「*( 2 + 1 - 1)'?既然'j'在技術上也是1?我試圖理解我們如何計算空間背後的邏輯。謝謝! – Dmitriy

1

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

我想這可能是你在找什麼。我希望它有幫助。

+0

我試圖用我的代碼完成它並應用到它,但使用'code'format(「」,4d)'code'或其他格式時無法獲得格式化工作,它會打破計劃。只是試圖修復我的。謝謝! – Dmitriy

+0

對不起,我的意思是指向你這個答案http://stackoverflow.com/a/13077777/6510412 我已經更新了我的原始答案。 – Paradoxinabox

+0

感謝您的鏈接。我正在尋找更多的解釋,因爲通過查看某人編寫的代碼很難說清楚。我試圖找出並學習如何使用字符串格式。 – Dmitriy

相關問題