列印乘法表

來源:互聯網
上載者:User
1.先列印8的乘法表:
for looper in [1,2,3,4,5]:    print looper," times 8 = ",looper*8
for looper in range (1,5):    print looper,"times 8 =",looper*8for looper in range (1,10):    print looper,"times 8 =",looper*8

結果:

>>> ================================ RESTART ================================>>> 1  times 8 =  82  times 8 =  163  times 8 =  244  times 8 =  325  times 8 =  40>>> 

 

 ================================ RESTART ================================>>> 1 times 8 = 82 times 8 = 163 times 8 = 244 times 8 = 32>>> ================================ RESTART ================================>>> 1 times 8 = 82 times 8 = 163 times 8 = 244 times 8 = 321 times 8 = 82 times 8 = 163 times 8 = 244 times 8 = 325 times 8 = 406 times 8 = 487 times 8 = 568 times 8 = 649 times 8 = 72>>> 

 

 

需注意,range(1,5)==[1,2,3,4]     而不等於【1,2,3,4,5】      range()提供一個數字列表,從給定的第一個數開始,在給定的最後一個數之前結束。

 

 

 

2.再列印一般數的乘法表 

 

x=int(raw_input("what multiplication table would you like?\n"))for i in range (1,11):  print x,"x",i,"=",x*iprint "\n\n***********************************************\n\n"
 
x=int(raw_input("what multiplication table would you like?\n"))m=int(raw_input("how high do you want to go ?\n"))for i in range (1,m+1):  print x,"x",i,"=",x*i  

運行結果:

 

>>> ================================ RESTART ================================>>> what multiplication table would you like?55 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 455 x 10 = 50***********************************************what multiplication table would you like?7how high do you want to go ?127 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 357 x 6 = 427 x 7 = 497 x 8 = 567 x 9 = 637 x 10 = 707 x 11 = 777 x 12 = 84>>> 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.