I have written a code to generate the pascal triangle. The following is a function from my code that deals with the spacing of the pascal triangle.
def print_pascals_triangle(triangle):
largest_element = triangle[-1][len(triangle[-1]) // 2]
element_width = len(str(largest_element))
def format_row(row):
return ' '.join([str(element).center(element_width) for element in row])
triangle_width = len(format_row(triangle[-1]))
for row in triangle:
print(format_row(row).center(triangle_width))
this gives me the following output:
Enter the number of rows you want in your Pascal`s Triangle: 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
As you can see the spacing is not perfect and my pascal triangle is not centered. How do i perfectly center my pascal triangle. Any help/tips is much appreciated. Thanks!
THE PERFECT PASCAL TRIANGLE!
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
6and28? In monospaced font is not possible to center them - there is no "half" whitespace character to shift it.