运用python输出杨辉三角
时间: 2023-03-30 22:00:25 浏览: 108
pythonpython实现杨辉三角的三种方法
以下是用 Python 输出杨辉三角的代码:
```python
def generate(numRows):
triangle = []
for i in range(numRows):
row = [1] * (i + 1)
for j in range(1, i):
row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j]
triangle.append(row)
return triangle
n = int(input("请输入要输出的行数:"))
result = generate(n)
for i in range(n):
print(" " * (n - i - 1), end="")
for j in range(i + 1):
print(result[i][j], end=" ")
print()
```
希望对你有帮助!
阅读全文