Python语言中的循环退出举例及while循环举例
小标 2018-12-20 来源 : 阅读 1651 评论 0

摘要:本文主要向大家介绍了Python语言中的循环退出举例及while循环举例,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言中的循环退出举例及while循环举例,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

循环退出 

for循环:

for

else

for 循环如果正常结束,都会执行else语句。


脚本1:

    #!/usr/bin/env python

    for i in xrange(10):

        print i

    else:

        print "main end"

结果:

    [root@localhost 20171227]# python exit.py

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    main end

    [root@localhost 20171227]#


脚本2:

    #!/usr/bin/env python

    import time

    for i in xrange(10):

        print i

    time.sleep(1)

    else:

        print "main end"

结果:(中途按ctrl+c中断)

    [root@localhost 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

    [root@localhost 20171227]#


脚本3:

没正常结束:

    #!/usr/bin/env python

    import time

    for i in xrange(10):

        print i

        if i == 5:

           break

    else:

        print "main end"

结果:

    [root@localhost 20171227]# python exit.py

    0

    1

    2

    3

    4

    5

    [root@localhost 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"

结果:

    [root@localhost 20171227]# python exit.py

    0

    1

    2

    4

    [root@localhost 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"

结果:

    [root@localhost 20171227]# python exit.py

    0

    1

    2

    4

    6

    [root@localhost 20171227]#


PIP显示第三方包

    [root@localhost 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

结果:

    [root@localhost 20171227]# python while.py

    0 hellow

    1 hellow

    2 hellow

    3 hellow

    4 hellow

    5 hellow

    6 hellow

    7 hellow

    8 hellow

    9 hellow

    [root@localhost 20171227]# 


脚本2:

    #!/usr/bin/python

    while 1:

        print 'hellow'

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

        if input == "q":

          break

结果:

    [root@localhost 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

    [root@localhost 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'

结果:

    [root@localhost 20171227]# python while.py

    hellow

    please input sth,q for exit.q

    world

    [root@localhost 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'


结果:   

    [root@localhost 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有所帮助,更多内容请关注职坐标编程语言Python频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved