如何在Python中格式化金字塔的两位数行?

如何在Python中格式化金字塔的两位数行?,python,python-3.x,Python,Python 3.x,白天好,晚上好!我有一个问题,我的数字金字塔底部行的格式与其他行不同。虽然我知道这是因为额外的数字,但我不知道该如何着手修复它 我尝试使用format()和repeation操作符来修复它,但没有任何效果。有人能给我另一个解决办法吗 pyramid_height = int(input("Enter the height you want your pyramid to be!\n")) row = 0 col = 0 row_len = 0 digit_len = len(

白天好,晚上好!我有一个问题,我的数字金字塔底部行的格式与其他行不同。虽然我知道这是因为额外的数字,但我不知道该如何着手修复它

我尝试使用format()和repeation操作符来修复它,但没有任何效果。有人能给我另一个解决办法吗

pyramid_height = int(input("Enter the height you want your pyramid to be!\n"))
row = 0
col = 0
row_len = 0
digit_len = len(str(pyramid_height))
#print(digit_len)
print("Printing pyramid:")
for row in range(1, pyramid_height+1): #for each row (starts at 0) in the range of 1- 3+1 (4)
    row_len +=row
    if (digit_len == 1):
        for col in range(1, pyramid_height-row+1): #Creates pyramid cols
            print(end = ' '*2) #Go through each col you want to make and put the nums in their place
            for col in range(row, 0, -1): #As row++, it controls the spacing between the nums 
                print(col, end = ' ')
            for col in range(2, row+1):
                print(col, end = ' ')
            print("")
    elif (digit_len == 2):
            for col in range(1, pyramid_height-row+1): 
                print(end = ' '*2)
            for col in range(row, 0, -1):
               print(format(col, "1d"), end = ' ')#-------------Affects the left side of the pyramid
            for col in range(2, row+1): #Controls the amount and formating of the top of the pyramid
                print(format(col, "1d"), end = ' ')
                #print(" ", end = '')#--------------------Affects the right side of the pyramid
            print(" " * len(str(col)))

您未能将两位数的输出调整为两位数的宽度:您的所有格式都是专门为一列设置的:

format(col, "1d")
你怎么会认为这会给你合适的间距

您必须处理附加宽度的所有格式:字段规格、插入的空白字符串等

elif (digit_len == 2):
        for col in range(1, pyramid_height-row+1): 
            print(end = '  '*2)
        for col in range(row, 0, -1):
           print(format(col, "2d"), end = '  ')#-------------Affects the left side of the pyramid
        for col in range(2, row+1): #Controls the amount and formating of the top of the pyramid
            print(format(col, "2d"), end = '  ')
            #print(" ", end = '')#--------------------Affects the right side of the pyramid
        print(" " * len(str(col)))
尺寸12的输出:

Enter the height you want your pyramid to be!
12
Printing pyramid:
                                             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    

你能展示一下金字塔本身吗?回答你的问题对我真的很有帮助