方向一
i = 1
while i <= 9:
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),end='')
j += 1
print()
i += 1
思路:先寫出列,從一到九,再寫出列,每次行=列的時候換行,行數增加一,所以裡面的while迴圈是 j <= i,當行數小於列數時,執行換行**,進行下一輪的迴圈,end = 『』 表示不換行繼續往下寫
方向一列印結果示例
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 =12 4*4 =16
1*5 = 5 2*5 =10 3*5 =15 4*5 =20 5*5 =25
1*6 = 6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36
1*7 = 7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49
1*8 = 8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64
1*9 = 9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81
方向二
i = 1
while i <= 9:
k = 1
while k <= 9 - i:
print(' ',end = ' ')
k += 1
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),end='')
j += 1
print()
i += 1
思路:方向二相對於方向一,需要在前面列印空格,且每次列印的空格跟行數有關,這時就需要乙個迴圈,使得前面的空格被列印出來
方向一列印結果示例
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 =12 4*4 =16
1*5 = 5 2*5 =10 3*5 =15 4*5 =20 5*5 =25
1*6 = 6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36
1*7 = 7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49
1*8 = 8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64
1*9 = 9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81
方向三
i = 9
while i >= 1:
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),end='')
j += 1
print()
i -= 1
思路:方向三中的i是從9到1,然後再是把i乙個乙個往下減
方向三列印結果示例
1*9 = 9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81
1*8 = 8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64
1*7 = 7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49
1*6 = 6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36
1*5 = 5 2*5 =10 3*5 =15 4*5 =20 5*5 =25
1*4 = 4 2*4 = 8 3*4 =12 4*4 =16
1*3 = 3 2*3 = 6 3*3 = 9
1*2 = 2 2*2 = 4
1*1 = 1
方向四
i = 9
while i >= 1:
k = 1
while k <= 9-i:
print(' ', end = ' ')
k += 1
j = 1
while j <= i
print('%d*%d = %2d'%( j,i ,i*j),end='')
j += 1
print()
i -= 1
思路:和方向二類似,在方向三的基礎上,在前面根據行數列印一定量的空格就可以了
方向四列印結果示例
1*9 = 9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81
1*8 = 8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64
1*7 = 7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49
1*6 = 6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36
1*5 = 5 2*5 =10 3*5 =15 4*5 =20 5*5 =25
1*4 = 4 2*4 = 8 3*4 =12 4*4 =16
1*3 = 3 2*3 = 6 3*3 = 9
1*2 = 2 2*2 = 4
1*1 = 1
python while迴圈 for迴圈
1變數的初始化 while 條件2 條件滿足時候 執行該 條件滿足時候 執行該 3變數的更新 1 while 迴圈輸出1 100所有的數 while 迴圈輸出20次我愛你 迴圈輸出1 100累加和 1 100之間所有數的和 1變數的初始化 i 0 sum 0 儲存和 判斷條件 while i 100...
python while 迴圈 if 迴圈
python 程式設計中 while 語句用於迴圈執行程式,即在某條件下,迴圈執行某段程式,以處理需要重複處理的相同任務。其基本形式為 執行語句可以是單個語句或語句塊。判斷條件可以是任何表示式,任何非零 或非空 null 的值均為true。當判斷條件假false時,迴圈結束。執行流程圖如下 prin...
Python while 迴圈列印菱形的2種方法
while作業 x,y數學方法 abs 函式返回的是數字的絕對值 利用x,y數學座標法 假設 x 的最小座標為 12 x 12 while x 12 當x小於最大座標時迴圈開始到最大結束期間24次迴圈 y與x基本相同 y 12 xy while y 12 座標絕對值相加如4個頂點 北 0,12 南 ...