用循环实现等边三角形,正方形,五边形,八边形,五角星
时间: 2024-04-29 12:22:41 浏览: 174
等边三角形:
```python
# 等边三角形
n = 6 # 边数
for i in range(1, n + 1):
print(' ' * (n - i), end='')
print('* ' * i)
```
输出:
```
*
* *
* * *
* * * *
* * * * *
* * * * * *
```
正方形:
```python
# 正方形
n = 6 # 边长
for i in range(n):
print('* ' * n)
```
输出:
```
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
```
五边形:
```python
# 五边形
n = 6 # 边长
for i in range(n):
print(' ' * (n - i - 1) + '* ' * (i + 1))
for i in range(n - 2, -1, -1):
print(' ' * (n - i - 1) + '* ' * (i + 1))
```
输出:
```
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
```
八边形:
```python
# 八边形
n = 6 # 边长
for i in range(n):
print(' ' * (n - i - 1) + '* ' * (i + 1) + ' ' * (2 * (n - i - 1)))
for i in range(n - 2, -1, -1):
print(' ' * (n - i - 1) + '* ' * (i + 1) + ' ' * (2 * (n - i - 1)))
```
输出:
```
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
```
五角星:
```python
# 五角星
n = 6 # 边长
for i in range(n):
print(' ' * (n - i - 1) + '* ' * (i + 1))
for i in range(n - 2, -1, -1):
print(' ' * (n - i - 1) + '* ' * (i + 1))
for i in range(n):
print(' ' * i + '* ' * (n - i))
for i in range(n - 2, -1, -1):
print(' ' * i + '* ' * (n - i))
```
输出:
```
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
* * * * *
* * * *
* * *
* *
*
```
阅读全文