0

我很困惑似乎是一个简单的问题。一切都被正确计算和显示。我希望它水平打印并居中。此外,删除括号和逗号的方法将是加号。

from math import factorial
numcheck = False
while numcheck == False:
try:
    height = (raw_input("Input height of Pascal's Triangle (must be integer): "))
    height = int(height)
    numcheck = True
except ValueError:
    print "Not an integer!"
pascaltri = [[factorial(n) // (factorial(k) * factorial(n - k)) for k in range(n+1)]
 for n in range(height)]
print pascaltri
4

1 回答 1

1

你可以这样格式化:

maxlen = len(" ".join([str(i) for i in pascaltri[-1]]))
formatted = '\n'.join([ " ".join([str(i) for i in line]).center(maxlen) for line in pascaltri])
print formatted

但是看看 Fredrik 的链接,有很多有趣的想法,包括更有效的生成三角形的方法。

于 2013-04-13T22:47:09.340 回答