python基础—for循环(图案打印) 2021-11-06

1,直角三角形

""" *
	**
	***
	****
	*****
	******
"""
layer = int(input("请输入您要打印的层数:"))
for i in range(1,layer+1):
	for j in range(1,i+1):
		print("*",end="")
	print()

python基础—for循环(图案打印) 2021-11-06
2,等腰三角形

"""
   *   				
  ***			
 *****
*******
"""
layer = int(input("请输入您要打印的层数:"))
for i in range(1,layer):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		print("*", end="")
	# 一行结束了,可以换行了
	print("")

python基础—for循环(图案打印) 2021-11-06
3,等腰倒三角形

"""
	*********
	 *******
	  *****
	   ***
	    *
"""
layer = int(input("请输入您要打印的层数:"))
for i in range(layer,0,-1):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		print("*", end="")
	# 一行结束了,可以换行了
	print("")

python基础—for循环(图案打印) 2021-11-06
4,实心菱形

"""
        *
	   ***
	  *****
	 *******
	*********
	 *******
	  *****
	   ***
	    *
"""
layer = int(input("请输入您要打印的层数:"))
while layer % 2 ==0:
	layer=int(input("对不起,层数必须是奇数层:"))
#上半层
for i in range(1,layer//2 + 2):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		print("*", end="")
	# 一行结束了,可以换行了
	print("")
#下半层
for i in range(layer//2,0,-1):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		print("*", end="")
	# 一行结束了,可以换行了
	print("")

python基础—for循环(图案打印) 2021-11-06
5,空心菱形

"""
        *
	   * *
	  *   *
	 *     *
	*       *
	 *     *
	  *   *
	   * *
	    *
"""
layer = int(input("请输入您要打印的层数:"))
while layer % 2 ==0:
	layer=int(input("对不起,层数必须是奇数层:"))
#上半层
for i in range(1,layer//2 + 2):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		if j ==1 or j==star_num:
			print("*", end="")
		else:
			print(" ",end="")
	# 一行结束了,可以换行了
	print("")
#下半层
for i in range(layer//2,0,-1):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		if j ==1 or j==star_num:
			print("*", end="")
		else:
			print(" ",end="")
	# 一行结束了,可以换行了
	print("")

python基础—for循环(图案打印) 2021-11-06
6,对角线菱形

"""
        *
	   ***
	  * * *
	 *  * *
	*********
	 *  *  *
	  * * *
	   ***
	    *
"""
layer = int(input("请输入您要打印的层数:"))
while layer % 2 ==0:
	layer=int(input("对不起,层数必须是奇数层:"))
#上半层
for i in range(1,layer//2 + 2):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		if j ==1 or j==star_num or j == (star_num +1)//2 or i == layer//2+1:
			print("*", end="")
		else:
			print(" ",end="")
	# 一行结束了,可以换行了
	print("")
#下半层
for i in range(layer//2,0,-1):
	# 先计算空格的数量
	space_num = layer - i
	for j in range(1, space_num + 1):
		print(" ", end="")
	# 计算*的数量
	star_num = 2 * i - 1
	for j in range(1, star_num + 1):
		if j ==1 or j==star_num or j == (star_num +1)//2:
			print("*", end="")
		else:
			print(" ",end="")
	# 一行结束了,可以换行了
	print("")

python基础—for循环(图案打印) 2021-11-06

上一篇:AUFS:多点合一,写时复制


下一篇:用html+css+js实现一个无限级树形控件