Python uses functions for Pascal's Triangle

running result:

 

 Specify the number of sectors in the function, corresponding to the output of Pascal's Triangle

Source code is as follows:

. 1  # - * - Coding: UTF-. 8 - * - 
2  '' ' 
. 3      chapter4_do.py
 . 4      function yanghui (n) for outputting a n-order Pascal's triangle
 . 5  ' '' 
. 6  DEF yanghui (n):
 . 7      IF  Not STR ( n) .isdecimal () or n <2 or n> 25 :
 . 8          Print ( " Pascal trigonometric yanghui (n), the parameter n must be not less than 2 and not more than a positive integer of 25 " )
 . 9          return False
 10      X = [ ]
 . 11      for I in Range (. 1, n-+. 1 ):
 12 is          x.append ([. 1] * I)
13     for i in range(2,n):
14         for j in range(1,i):
15             x[i][j]=x[i-1][j-1]+x[i-1][j]
16 
17     for i in range(n):
18         if n<=10:print(' '*(40-4*i),end='')
19         for j in range(i+1):
20             print('%-8d'%x[i][j],end='')
21         print()
22 is  
23 is  
24  IF  the __name__ == ' __main__ ' :
 25      Print ( " modules run independently from the test output: " )
 26 is      Print ( " a, 10 Pascal's Triangle following order: " )
 27      yanghui (10)

 

Guess you like

Origin www.cnblogs.com/yijiahao/p/11828059.html