python金字塔while循环
时间: 2023-08-21 07:17:10 浏览: 168
金字塔模式可以使用while循环来实现。以下是一个使用while循环打印金字塔模式的Python代码:
```python
n = 5
i = 0
while(i <= n):
print(" " * (n - i) + "*" * i)
i += 1
```
这段代码会打印出一个金字塔模式,其中每一行的星号数量逐渐增加。\[1\]
另外,你也可以使用for循环来实现金字塔模式。以下是一个使用for循环打印金字塔模式的Python代码:
```python
height = 5
for row in range(1, height + 1):
print(" " * (height - row) + "*" * row)
```
这段代码同样会打印出一个金字塔模式,其中每一行的星号数量逐渐增加。\[3\]
希望这个回答对你有帮助!
#### 引用[.reference_title]
- *1* *2* *3* [Python 教程之控制流(6)打印金字塔图案的程序](https://blog.csdn.net/m0_73720982/article/details/127073857)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]