本帖最後由 鄭繼威 於 2023-1-31 14:17 編輯
一個三層的巢狀迴圈,當外層執行2次,中層執行3次,內層執行4次,總共會執行24次。
- sum=0
- for i in range(1,3):
- for j in range(1,4):
- for k in range(1,5):
- sum+=1
- print("第%d次: %d %d %d"%(sum,i,j,k))
複製代碼- n=0
- for i in range(1,3):
- for j in range(1,4):
- for k in range(1,5):
- n+=1;
- print("第{}次: {} {} {}".format(n,i,j,k))
複製代碼- print("i j k")
- counter=1
- for i in range(1,3):
- for j in range(1,4):
- for k in range(1,5):
- print(f"第{counter}次{i} {j} {k}")
- counter=counter+1
複製代碼 |