我对看似简单的问题感到非常难过。一切都是正确计算和显示的。我想让它水平打印并居中。此外,删除括号和逗号的方法将是加号。
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
答案 0 :(得分: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的链接,有很多有趣的想法,包括更有效的方法来生成三角形。