Python中的迴圈退出舉例及while迴圈舉例

來源:互聯網
上載者:User

標籤:python中的迴圈退出舉例及while

迴圈退出 

for迴圈:

for

else

for 迴圈如果正常結束,都會執行else語句。


指令碼1:

    #!/usr/bin/env python

    for i in xrange(10):

        print i

    else:

        print "main end"

結果:

    [[email protected] 20171227]# python exit.py

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    main end

    [[email protected] 20171227]#


指令碼2:

    #!/usr/bin/env python

    import time

    for i in xrange(10):

        print i

    time.sleep(1)

    else:

        print "main end"

結果:(中途按ctrl+c中斷)

    [[email protected] 20171227]# python exit.py

    0

    1

    2

    3

    4

    ^CTraceback (most recent call last):

    File "exit.py", line 6, in <module>

    time.sleep(1)

    KeyboardInterrupt

    [[email protected] 20171227]#


指令碼3:

沒正常結束:

    #!/usr/bin/env python

    import time

    for i in xrange(10):

        print i

        if i == 5:

           break

    else:

        print "main end"

結果:

    [[email protected] 20171227]# python exit.py

    0

    1

    2

    3

    4

    5

    [[email protected] 20171227]#


指令碼4:(跳過本次迴圈continue)

    #!/usr/bin/env python

    import time

    for i in xrange(10):

        if i == 3 :

          continue

        if i == 5:

            break

        print i

    else:

        print "main end"

結果:

    [[email protected] 20171227]# python exit.py

    0

    1

    2

    4

    [[email protected] 20171227]#


指令碼5:(pass 什麼都不作)

    #!/usr/bin/env python

    import time

    for i in xrange(10):

        if i == 3 :

            continue

        elif i == 5:

            break

        elif i ==6:

           pass   #類似shell 中的:

        print i

    else:

        print "main end"

指令碼6:

    #!/usr/bin/env python

    import time

    import sys

    for i in xrange(10):

        if i == 3 :

            continue

        elif i == 5:

            continue

        elif i ==6:

            pass

        elif i ==7:

            sys.exit()

        print i

    else:

        print "main end"

    print "hahaha"

結果:

    [[email protected] 20171227]# python exit.py

    0

    1

    2

    4

    6

    [[email protected] 20171227]#


PIP顯示第三方包

    [[email protected] 20171227]# pip list

    DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

    backports.ssl-match-hostname (3.4.0.2)

    chardet (2.2.1)

    configobj (4.7.2)

    decorator (3.4.0)

    iniparse (0.4)

    IPy (0.75)

    ipython (1.2.1)

    jsonschema (2.5.1)

    kitchen (1.1.1)

    langtable (0.0.31)

    matplotlib (1.2.0)


作業:猜數字遊戲

    系統產生一個20以內的隨機整數。

    玩家有6次機會進行猜猜看,每次猜測都有反饋(猜大了,猜小了,猜對了-結束)

    6次中,猜對了,玩家贏了。

    否則系統贏了

    In [1]: import random

    In [2]: random.ran

    random.randint    random.random     random.randrange

    In [2]: random.randint(1,20)

    Out[2]: 14

    In [3]: random.randint(1,20)

    Out[3]: 6


流程式控制制-while舉例

while與for相對比:

    for迴圈用在有次數的迴圈上。

    while迴圈用在有條件的控制上。

while迴圈:

    while迴圈,直到運算式變為假,才退出。while迴圈,運算式是一個邏輯運算式,必須返回一個True或False

文法:

    while expression:

    statement(s)

練習指令碼如果下:


指令碼1:

    #!/usr/bin/python

    n = 0

    while 1:

        if n == 10:

            break

        print n, 'hellow'

        n +=1

結果:

    [[email protected] 20171227]# python while.py

    0 hellow

    1 hellow

    2 hellow

    3 hellow

    4 hellow

    5 hellow

    6 hellow

    7 hellow

    8 hellow

    9 hellow

    [[email protected] 20171227]# 


指令碼2:

    #!/usr/bin/python

    while 1:

        print 'hellow'

        input=raw_input("please input sth,q for exit.")

        if input == "q":

          break

結果:

    [[email protected] 20171227]# python while.py

    hellow

    please input sth,q for exit.s

    hellow

    please input sth,q for exit.3

    hellow

    please input sth,q for exit.q

    [[email protected] 20171227]#


條件 為假時也會退出:

指令碼3:

    #!/usr/bin/python

    sth=''

    while sth != 'q':

        print 'hellow'

        sth=raw_input("please input sth,q for exit.")

指令碼4:

斷行符號後退出:

    #!/usr/bin/python

    sth=''

    while sth != 'q':

        print 'hellow'

        sth=raw_input("please input sth,q for exit.")

        if not sth:

            break

指令碼5:

    #!/usr/bin/python

    sth=''

    while sth != 'q':

        print 'hellow'

        sth=raw_input("please input sth,q for exit.")

        if not sth:

            break

        else:

            print 'world'

結果:

    [[email protected] 20171227]# python while.py

    hellow

    please input sth,q for exit.q

    world

    [[email protected] 20171227]#


指令碼6:

#!/usr/bin/python

sth=''

while sth != 'q':

    print 'hellow'

    sth=raw_input("please input sth,q for exit.")

    if not sth:

        break

    if sth == 'quit':

        continue

    print 'continue'

else:

    print 'world'


結果:   

    [[email protected] 20171227]# python while.py    

    hellow

    please input sth,q for exit.ss

    continue

    hellow

    please input sth,q for exit.df

    continue

    hellow

    please input sth,q for exit.quit

    hellow

    please input sth,q for exit.q

    continue

    world

    

Python中的迴圈退出舉例及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.