1、打印如下图形
*
**
***
****
*****
******
代码如下:
layer = int(input("请输入你要打印的层数:"))
index = 1
while index <= layer: #layer = 3
#打印每层的*
j = 1
while j <= index: #index = 1 2
print("*",end="")
j += 1
print() #此处的意思为换行
index += 1
2、打印如下图形
~~~* 此处的~表示空格
~~***
~*****
*******
代码如下:
layer = int(input("请输入你要打印的行数:"))
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("")
3、打印如下图形
* 第一行 3个空格(~)
***
*****
*******
*****
***
*
代码如下:
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): # (3,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("")
4、打印如下图形
*
* *
* *
* *
* *
* *
*
代码如下:
layer = int(input("请输入你要打印的行数:"))
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):
#判断是不是第一个和最后一个
if j == 0 or j == star_num - 1:
print("*",end="")
else:
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):
#判断是不是第一个和最后一个
if j == 0 or j == star_num - 1:
print("*",end="")
else:
print(" ",end="")
print("")
5、打印如下图形
*
* *
* *
*******
* *
* *
*
代码如下:
layer = int(input("请输入你要打印的行数:"))
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):
#判断是不是第一个和最后一个
if j == 0 or j == star_num - 1 or i == layer // 2 + 1: #i == layer // 2
+ 1 表示最后一行
print("*",end="")
else:
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):
#判断是不是第一个和最后一个
if j == 0 or j == star_num - 1:
print("*",end="")
else:
print(" ",end="")
print("")
6、打印如下图形
*
***
* * *
*******
* * *
***
*
代码如下:
layer = int(input("请输入你要打印的行数:"))
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):
#判断是不是第一个和最后一个
if j == 0 or j == star_num - 1 or i == layer // 2 + 1 or j ==
star_num//2: #j == star_num//2 判断的是中间那一列
print("*",end="")
else:
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):
#判断是不是第一个和最后一个
if j == 0 or j == star_num - 1 or j == star_num//2:
print("*",end="")
else:
print(" ",end="")
print("")
7、打印如下图形
*
***
* * *
*******
*
*
*
代码如下:
layer = int(input("请输入你要打印的行数:"))
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):
#判断是不是第一个和最后一个
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):
#计算空格的个数
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):
#判断是不是第一个和最后一个
if j == star_num//2:
print("*",end="")
else:
print(" ",end="")
print("")