layer = int(input("请输入打印的行数:"))
while layer % 2 == 0:
layer = int(input("对不起,请输入奇数行:"))
#上半部分 分为两个等腰三角形
for i in range(1,layer // 2 + 2):#上半部分多打印一行 layer=5
#计算出空格的个数
space_num = layer - i;
for j in range(0,space_num):
print (" ",end="")
#计算*个数
star_num = 2 * i -1
for j in range(0,star_num): #j矛盾吗
if(j== 0 or j== star_num-1 or i== layer//2+1 or j==star_num//2):
print("*",end='')
else:
print(" ",end='')
print("")
#下半部分
for i in range(layer //2 , 0,-1):
#计算出空格的个数
space_num = layer - i;
for j in range(0,space_num):
print (" ",end="")
#计算*个数
star_num = 2 * i -1
for j in range(0,star_num): #j矛盾吗
if( i== layer//2+1 or j==star_num//2):
print("*",end='')
else:
print(" ",end='')
print("")
Python菱形
最新推荐文章于 2022-11-27 16:03:47 发布