Python(1)—— 打印不同类型三角形(while循环)

1.直角三角形

类型(1):

*	*	*	*	*	
*	*	*	*	
*	*	*	
*	*	
*	
i = 1 # 预先规定行数
while i < 6: # 设定条件1,以保证行数为5,在第六行i=0停止运行
    j = 1 # 给变量j赋值为0
    while j < 7-i : # 规定条件2,以保证每一行*的数量与行数相同
        print("*",end="\t") # \t 为ASCII水平制表符,end=""是为了不让代码自动换行
        j +=1 # 使每一行的*的数量增长
    print("") # 让每一行换到下一行
    i += 1 #  使行数不断地增长

类型(2):

*	
*	*	
*	*	*	
*	*	*	*	
*	*	*	*	*	
line = 5
i=1
while i <= line:
    j =1
    while j <= i:
        print("*", end = "\t")
        j+=1
    print("")
    i+=1

类型(3):

 	 	 	 	*	
 	 	 	*	*	
 	 	*	*	*	
 	*	*	*	*	
*	*	*	*	*	
line = 5
i=1
while i <= line:
    j = 1
    # 内循环
    while j < 6-i: # 空格的数量
        print(" ", end="\t")
        j += 1
    k = 1 # * 的数量
    while k < i + 1:
        print("*", end="\t")
        k += 1
    print("")
    i+=1

注:对调 j 和 k 的位置打印出来的结果为类型(2)

类型(4):

*	*	*	*	*	
 	*	*	*	*	
 	 	*	*	*	
 	 	 	*	*	
 	 	 	 	*	
line = 5
i=1
while i <= line:
    j = 1
    while j < i:
        print(" ", end="\t")
        j += 1
    k = 1
    while k <= line + 1 - i:
        print("*", end="\t")
        k += 1
    print("")
    i+=1

2.等腰三角形

类型(1):

 	 	 	 	*	
 	 	 	*	*	*	
 	 	*	*	*	*	*	
 	*	*	*	*	*	*	*	
*	*	*	*	*	*	*	*	*	
line = 5
i = 1
while i <= line:
    j = 1
    while j <= line - i:
        print(" ", end="\t")
        j += 1
    k = 1
    while k <= 2 * i - 1:
        print("*", end="\t")
        k += 1
    print("")
    i += 1

类型(2):

 	*	*	*	*	*	*	*	*	*	
 	 	*	*	*	*	*	*	*	
 	 	 	*	*	*	*	*	
 	 	 	 	*	*	*	
 	 	 	 	 	*	
line = 5
i = 1
while i <= line:
    j = 1
    while j <= i:
        print(" ", end="\t")
        j += 1
    k = 1
    while k <= 2*(line + 1 -i) - 1:
        print("*", end="\t")
        k += 1
    print("")
    i += 1

你可能感兴趣的:(Python)