用python编写程序打印输出下面图案。 * *** ***** *******
时间: 2023-11-25 20:07:08 浏览: 665
以下是两种Python编写程序打印输出图案的例子:
1. 打印输出下面图案:
```
*
***
*****
*******
```
代码如下:
```python
layer = 4
for i in range(1,layer+1):
#计算空格的个数
spce_num = layer - i
for j in range(0,spce_num):
print(" ",end="")
#计算*个数
star_num = 2 * i - 1
for j in range(0,star_num):
print("*",end="")
print("")
```
2. 打印输出下面图案:
```
*
***
*****
*******
*****
***
*
```
代码如下:
```python
layer = 7
while layer % 2 == 0:
layer = int(input("请输入正确的要打印的行数,注意是奇数行:"))
#上半部分
for i in range(1,layer // 2 + 2):
#计算空格的个数
spce_num = layer - i
for j in range(0,spce_num):
print(" ",end="")
#计算*个数
star_num = 2 * i - 1
for j in range(0,star_num):
print("*",end="")
print("")
#下半部分
for i in range(layer // 2 ,0,-1):
#计算空格的个数
spce_num = layer - i
for j in range(0,spce_num):
print(" ",end="")
#计算*个数
star_num = 2 * i - 1
for j in range(0,star_num):
print("*",end="")
print("")
```
阅读全文