利用for循環打印三角形,菱形,字母等
阿新 • • 發佈:2018-06-22
none 自己 img IV break opened 打印 訓練 更改
三角形:
# 打印等腰三角形 # 控制行 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="View Code") #控制列,規則是第一行一列,第二行二列 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()
字母:
while 1: def A_zm(): for i in range(1,6): #更改打印起始位置 for k in range(1,6-i):View Codeprint(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("輸入的字母沒有")
菱形:
# 菱形 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!
利用for循環打印三角形,菱形,字母等