for 循环 与 while 循环
Posted wqq0723
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for 循环 与 while 循环相关的知识,希望对你有一定的参考价值。
for 循环 与 while 循环
-
打印三角形
1 # 打印三角形 2 # * 3 # * * 4 # * * * 5 # * * * * 6 # * * * * * 7 8 # 使用 for 循环 9 for i in range(5): 10 print("* " * (i + 1)) 11 12 # 使用 while 语句 13 i = 1 14 while i < 6: 15 j = 0 16 while j < i: 17 print("*", end=‘ ‘) 18 j += 1 19 print(" ") 20 i += 1
-
打印空心三角形
1 # 打印空心三角形 2 # * 3 # * * 4 # * * 5 # * * 6 # * * * * * 7 #for 循环 8 for i in range(0, 5): 9 for j in range(i+1): 10 if i == 4: 11 print("* ",end=‘‘) 12 continue 13 if j == 0 or j == i: 14 print("* ",end=‘‘) 15 else: 16 print(" ",end=‘‘) 17 print( ) 18 19 #while 循环 20 i = 1 21 while i < 6: 22 j = 0 23 while j < i: 24 j += 1 25 if i == 5: 26 print("* ", end=‘‘) 27 continue 28 if j == 1 or j == i: 29 print("* ", end=‘‘) 30 else: 31 print(" ", end=‘‘) 32 print( ) 33 i += 1
-
打印金字塔
1 # 打印金字塔 2 # * 3 # * * 4 # * * * 5 # * * * * 6 # * * * * * 7 #for 循环 8 for i in range(5): 9 for k in range(4-i): 10 print("",end=" ") 11 for j in range(i+1): 12 print("*",end=" ") 13 print("") 14 15 #while 循环 16 i = 0 17 while i<5: 18 k = 0 19 while k < 5-i: 20 print("",end=" ") 21 k += 1 22 j = 0 23 while j < i+1: 24 print("*",end=" ") 25 j += 1 26 i += 1 27 print("")
-
打印倒三角
1 # 打印倒三角 2 # * * * * * 3 # * * * * 4 # * * * 5 # * * 6 # * 7 #for 循环 8 for i in range(5): 9 for k in range(i): 10 print("",end=" ") 11 for j in range(5-i): 12 print("*",end=" ") 13 print("") 14 15 #while 循环 16 i = 0 17 while i<5: 18 k = 0 19 while k < i: 20 print("",end=" ") 21 k += 1 22 j = 0 23 while j < 5-i: 24 print("*",end=" ") 25 j += 1 26 i += 1 27 print("")
-
打印菱形
1 # 打印菱形 2 # * 3 # * * 4 # * * * 5 # * * * * 6 # * * * * * 7 # * * * * 8 # * * * 9 # * * 10 # * 11 12 #for 循环 13 for i in range(5): 14 for k in range(4-i): 15 print("",end=" ") 16 for j in range(i+1): 17 print("*",end=" ") 18 print("") 19 for i in range(4): 20 for k in range(i+1): 21 print("",end=" ") 22 for j in range(4-i): 23 print("*",end=" ") 24 print("") 25 26 #while 循环 27 i = 0 28 while i<5: 29 k = 0 30 while k < 5-i: 31 print("",end=" ") 32 k += 1 33 j = 0 34 while j < i+1: 35 print("*",end=" ") 36 j += 1 37 i += 1 38 print("") 39 i = 0 40 while i<4: 41 k = 0 42 while k < i+2: 43 print("",end=" ") 44 k += 1 45 j = 0 46 while j < 4-i: 47 print("*",end=" ") 48 j += 1 49 i += 1 50 print("")
-
打印空心菱形
1 # 打印空心菱形 2 # * 3 # * * 4 # * * 5 # * * 6 # * * 7 # * * 8 # * * 9 # * * 10 # * 11 12 #for 循环 13 for i in range(5): 14 for k in range(4-i): 15 print("",end=" ") 16 for j in range(i+1): 17 if i == 5: 18 print("*",end=" ") 19 continue 20 if j == 0 or j == i: 21 print("*",end=" ") 22 else: 23 print(" ",end=" ") 24 print("") 25 for i in range(4): 26 for k in range(i+1): 27 print("",end=" ") 28 for j in range(4-i): 29 if i == 3: 30 print("*",end=" ") 31 continue 32 if j == 0 or j == 3-i: 33 print("*",end=" ") 34 else: 35 print(" ",end=" ") 36 print("")
-
打印九九乘法表
1 # 打印九九乘法表 2 # 1 * 1 = 1 3 # 4 # 2 * 1 = 2 2 * 2 = 4 5 # 6 # 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 7 # 8 # 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 9 # 10 # 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 11 # 12 # 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 13 # 14 # 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 15 # 16 # 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 17 # 18 # 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 19 20 # for 循环 21 for i in range(1, 10): 22 for j in range(1, i + 1): 23 print("%d * %d = %d " % (i, j, i * j), end=‘ ‘) 24 print(" ") 25 26 # while 循环 27 i = 1 28 while i < 10: 29 j = 1 30 while j <= i: 31 print("%d * %d = %d" % (i, j, i * j), end=‘ ‘) 32 j += 1 33 print(" ") 34 i += 1
-
求水仙花数
#for 循环 for i in range(100,1000): x = i // 100 y = i // 100 % 10 z = i % 10 if x*x*x + y*y*y + z*z*z == i: print(i)
以上是关于for 循环 与 while 循环的主要内容,如果未能解决你的问题,请参考以下文章