Python練習題(1)

來源:互聯網
上載者:User

標籤:while迴圈   迴圈輸出   ==   pre   ...   nbsp   while   等於   輸出   

<1> 使用while迴圈輸出數字 1  2  3  4  5  6   8  9  10

1 a = 02 while a < 10:3     a = a + 14     if a == 7:5         pass6     else:7         print(a)

<2>求1-100的所有數的和

1 a = 02 b = 03 while a < 100:4     a = a + 1     #每迴圈一次,a會自動加一5     b = b + a     #每次迴圈b會記錄當前a的值,並與a相加6 print(b)

<3>輸出1-100以內的所有奇數

1 a = 02 while a < 100:3     a = a +14     if a % 2 == 0:   #如果除2求於等於05         pass              #pass佔位,什麼都不做6     else:                  #否則輸出奇數7         print(a)

<4>輸出1-100以內的所有偶數

1 a = 02 while a < 100:3     a = a +14     if a % 2 == 0:5         print(a)

<5>求1-2+3-4+5-6+7 ... 99的所有數的和

 1 # -*- coding:utf-8 -*- 2 #求1-2+3-4+5...99的所有數的和 3 ‘‘‘ 4 找規律奇數是加,偶數是減 5 ‘‘‘ 6 a = 1 7 b = 0 8 while a  <100: 9     temp = a  % 210     if temp ==0:11         b = b - a12     else:13         b = b + a 14     a += 115 print(b)

 

Python練習題(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.