Triangle pascals in python?

Good, so I have been working on this for a long time, and I can’t get it. Our task was to make a triangle and the center of Pascal, and all this is a good thing ... But I can not understand it.

def factorial(n): if (n <= 1): return 1 else: return n * factorial(n-1) def combination(n, k): return int (factorial(n) / (factorial(k) * factorial(nk))) def pascal_row(row): answer = "" for entry in range(row+1): answer = answer + " " + str(combination(row, entry)) print answer def pascal_triangle(rows): for row in range(rows): pascal_row(row) pascal_triangle(10) 

I know that if I make the last line, which is 9 and subtract the current line, and then multiply by three, it will give me the correct spacing for each line. I'm just not sure how to include this in code? If you could help me, that would be fantastic! Thanks for the help in advance.

-1
source share
1 answer

You can do the following. For more examples and documentation on string formatting, visit http://docs.python.org/2/library/string.html#format-examples

 print("{:^50}".format(pascal_row(row))) 

In the above code, ^ centers the string data. 50 means line length. (Large enough to span the longest line)

+1
source

Source: https://habr.com/ru/post/1314106/


All Articles