利用for循环打印三角形,菱形,字母等

三角形:

# 打印等腰三角形
# 控制行
for i in range(1,6):
    #更改打印起始位置
    for k in range(1,6-i):
        print(end=" ")
    #控制列,规则是第一行一列,第二行二列
    for j in range(6-i,6):
        print("*",end = " ")
    print()
print("="*20)
# 空心
# 控制行
for i in range(1,6):
    #更改打印起始位置
    for k in range(1,6-i):
        print(end=" ")
    #控制列,规则是第一行一列,第二行二列
    for j in range(6-i,6):
        if i==1 or i==5 or j==6-i or j==5:
            print("*",end = " ")
        else:
            print(' ',end=" ")
    print()
View Code

字母:

while 1:
    def A_zm():
        for i in range(1,6):
        #更改打印起始位置
            for k in range(1,6-i):
                print(end=" ")
        #控制列,规则是第一行一列,第二行二列
            for j in range(6-i,6):
                if i==1 or i==4 or j==6-i or j==5:
                    print("*",end = " ")
                else:
                    print(' ',end=" ")
            print()
    def B_zm():
        for m in range(1,5):
            for n in range(1,4):
                if n==1:
                    print("*",end=" ")
                elif m==1 or m==4:
                    if n>2:
                        break
                    else:
                        print("*",end=" ")
                elif n>2:
                    if m==2 or m==3:
                        print("*",end=" ")

                else:
                    print(" ",end=" ")
            print()
        for m in range(1,5):
            for n in range(1,4):
                if n==1:
                    print("*",end=" ")
                elif m==1 or m==4:
                    if n>2:
                        break
                    else:
                        print("*",end=" ")
                elif n>2:
                    if m==2 or m==3:
                        print("*",end=" ")

                else:
                    print(" ",end=" ")
            print()
    
    
    def P_zm():
        for m in range(1,7):
            for n in range(1,4):
                if n==1:
                    print("*",end=" ")
                elif m==1 or m==4:
                    if n>2:
                        break
                    else:
                        print("*",end=" ")
                elif n>2:
                    if m==2 or m==3:
                        print("*",end=" ")

                else:
                    print(" ",end=" ")
            print()
    def R_zm():
        for m in range(1,7):
            for n in range(1,4):
                if n==1:
                    print("*",end=" ")
                elif m==1 or m==4:
                    if n>2:
                        break
                    else:
                        print("*",end=" ")
                elif n>2:
                    if m==2 or m==3:
                        print("*",end=" ")

                else:
                    print(" ",end=" ")
            print()
    
    share = input("请输入要打印的字母:")
    if share == "-1":
        print("程序结束")
        break
        
    elif share == "A":
        A_zm()
    elif share == "B":
        B_zm()
    elif share == "R":
        R_zm()
    elif share == 'P':
        P_zm()
    else:
        print("输入的字母没有")
View Code

菱形:

# 菱形 
for i in range(6):
    #更改打印起始位置
    for k in range(6-i):
        print(end=" ")
    #控制列,规则是第一行一列,第二行二列
    for j in range(6-i,6):
        if i==0  or j==6-i or j==5:
            print("*",end = " ")
        else:
            print(' ',end=" ")
    print()   

for i in range(6):
    #更改打印起始位置
    for k in range(i):
        print(end=" ")
    #控制列,规则是第一行一列,第二行二列
    for j in range(6-i):
        if j==5-i  or i==5 or j==0:
            print("*",end = " ")
        else:
            print(' ',end= " " )
        
     
    print()
View Code

 

多多训练自己的逻辑思维结构。fighting!

转载于:https://www.cnblogs.com/llj0403/p/9211469.html

你可能感兴趣的:(利用for循环打印三角形,菱形,字母等)