python 基礎 2.4 while 迴圈

來源:互聯網
上載者:User

標籤:應用   支援   something   enc   lease   input   code   pre   2.4   

#/usr/bin/python 
#coding=utf-8
#@Time :2017/10/18 15:31
#@Auther :liuzhenchuan
#@File :while 迴圈.py

樣本1:
1 n = 02 while True:3     print ‘hello world‘4     if n == 10:5         break6     n +=1
列印如下:
 1 hello world 2 hello world 3 hello world 4 hello world 5 hello world 6 hello world 7 hello world 8 hello world 9 hello world10 hello world11 hello world

樣本2:

1 n = 02 while True:3     if n==10:4         break5     print n ,‘hello‘6     n +=1

列印如下:

 1 0 hello 2 1 hello 3 2 hello 4 3 hello 5 4 hello 6 5 hello 7 6 hello 8 7 hello 9 8 hello10 9 hello


樣本3:

#條件為假的時候,也是退出迴圈.輸入Null 字元的時候也退出迴圈
1 rinput = ‘‘2 while rinput !=‘q‘:3     rinput = raw_input(‘please input something,q is quite: ‘)4     print ‘hello‘5     if rinput == ‘‘:6         break


樣本4:

#應用邏輯非判斷,退出while迴圈。while條件為假時,退出while迴圈
1 rinput = ‘‘2 while rinput !=‘q‘:3     rinput = raw_input(‘please input something,q is quite: ‘)4     print ‘hello‘5     if not rinput :6         break

 

樣本5:

# while...else...:正常結束while迴圈時會列印else後面的語句.與for...else...迴圈體一樣
1 rinput = ‘‘2 while rinput !=‘q‘:3     rinput = raw_input(‘please input something,q is quite: ‘)4     print ‘hello‘5     if not rinput :6         break7 else:8     print ‘hello world‘

 

樣本6:

#while 迴圈嵌套if迴圈體,支援contine
 1 rinput = ‘‘ 2 while rinput !=‘q‘: 3     rinput = raw_input(‘please input something,q is quite: ‘) 4     print ‘hello‘ 5     if not rinput : 6         break 7     elif rinput == ‘quite‘: 8         continue 9         print ‘continue‘10 else:11     print ‘hello world‘

 

 

 

 

 

 

 


 



python 基礎 2.4 while 迴圈

聯繫我們

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