標籤:style print 乘法 嵌套迴圈 color end range 初學 nbsp
分別應用while和for的嵌套迴圈,適用於初學的人看看
x = 1while x <= 9: y = 1 while y <= x: print (y,‘*‘,x,‘=‘,x*y,end="\t") y += 1 print ("") x += 1
執行結果:
1 * 1 = 1
1 * 2 = 22 * 2 = 41 * 3 = 32 * 3 = 63 * 3 = 91 * 4 = 42 * 4 = 83 * 4 = 124 * 4 = 161 * 5 = 52 * 5 = 103 * 5 = 154 * 5 = 205 * 5 = 251 * 6 = 62 * 6 = 123 * 6 = 184 * 6 = 245 * 6 = 306 * 6 = 361 * 7 = 72 * 7 = 143 * 7 = 214 * 7 = 285 * 7 = 356 * 7 = 427 * 7 = 491 * 8 = 82 * 8 = 163 * 8 = 244 * 8 = 325 * 8 = 406 * 8 = 487 * 8 = 568 * 8 = 641 * 9 = 92 * 9 = 183 * 9 = 274 * 9 = 365 * 9 = 456 * 9 = 547 * 9 = 638 * 9 = 729 * 9 = 81
for i in range(1,10): for j in range(1,1+i): print ("%dx%d=%d\t"%(j,i,i*j),end="") print ("")
執行結果:
1x1=11x2=22x2=41x3=32x3=63x3=91x4=42x4=83x4=124x4=161x5=52x5=103x5=154x5=205x5=251x6=62x6=123x6=184x6=245x6=306x6=361x7=72x7=143x7=214x7=285x7=356x7=427x7=491x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=641x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=81
python兩則99乘法表