8、 输出菱形, (用单循环和循环嵌套两种形式实现 )
时间: 2024-02-06 15:09:15 浏览: 66
使用单循环实现菱形:
```python
n = 5 # 菱形大小
for i in range(n):
print(" "*(n-i-1) + "* "*(i+1))
for i in range(n-1):
print(" "*(i+1) + "* "*(n-i-1))
```
使用循环嵌套实现菱形:
```python
n = 5 # 菱形大小
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for j in range(i+1):
print("* ", end="")
print()
for i in range(n-1):
for j in range(i+1):
print(" ", end="")
for j in range(n-i-1):
print("* ", end="")
print()
```
两种实现方式都可以得到以下输出结果:
```
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
```
阅读全文