重學Python,學python

來源:互聯網
上載者:User

重學Python,學python
if語句應用之——求最大值

 1 """ 2 求三個數字中的最大值,並列印出來 3 """ 4 a = int(input("a:")) 5 b = int(input("b:")) 6 c = int(input("c:")) 7  8 if a>b: 9     max_num = a10     if max_num >c:11         print(max_num)12     else:13         print(c)14 else:15     max_num = b16     if max_num > c:17         print(max_num)18     else:19         print(c)20 21 print(max(a,b,c))  #內建函數22 print(min(a,b,c))   #內建函數
 while迴圈

 

例題1:輸出1到100之間的偶數-(不過我覺得我的程式好蠢)

 1 """ 2 輸出1到100間的偶數 3 """ 4 a = 0 5 while a < 101: 6     for i in range(101): 7         if i%2 == 0: 8             print(i) 9         i += 110     if i > 100:11         break12         

簡化的:

a = 0 while a < 101:    if a %2 == 0:        print(a)    a += 1

注意: while 後的條件判斷語句,一定要在程式中做flase打斷,不然會無休止的運行下去

 

聯繫我們

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