python输出左对齐杨辉三角
时间: 2023-10-02 22:05:34 浏览: 166
用Python输出一个杨辉三角的例子
可以使用以下代码输出左对齐的杨辉三角:
```python
def pascal_triangle(n):
# 初始化杨辉三角的第一行
row = [1]
# 输出第一行
print(row)
# 循环输出杨辉三角的每一行
for i in range(1, n):
# 计算当前行
current_row = [1]
for j in range(1, i):
current_row.append(row[j-1]+row[j])
current_row.append(1)
# 输出当前行
print(current_row)
# 更新上一行
row = current_row
# 输出10行左对齐的杨辉三角
pascal_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]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
```
阅读全文