Python 程式,根據使用者給出的行數列印帕斯卡三角形
如果需要為特定數量的行列印帕斯卡三角形,並且該數字由使用者輸入,則使用一個簡單的“for”迴圈。
以下是相同的演示 -
示例
from math import factorial
input = int(input("Enter the number of rows..."))
for i in range(input):
for j in range(input-i+1):
print(end=" ")
for j in range(i+1):
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")
print()產出
Enter the number of rows...6 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
說明
匯入必需的包。
從使用者那裡獲得行數作為輸入。
以巢狀迴圈的形式對數字進行迭代。
因子函式用於在控制檯上列印帕斯卡三角形。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP