1,直角三角形
""" * ** *** **** ***** ****** """ layer = int(input("请输入您要打印的层数:")) for i in range(1,layer+1): for j in range(1,i+1): print("*",end="") print()
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("")
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("")
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("")
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("")
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("")