广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python中的循环退出举例及while
  • 484
分享到

Python中的循环退出举例及while

Python 2023-01-31 02:01:03 484人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

循环退出 for循环:forelsefor 循环如果正常结束,都会执行else语句。脚本1:    #!/usr/bin/env python    for i in xrange(10):        print i    else: 

循环退出 

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

    

--结束END--

本文标题: Python中的循环退出举例及while

本文链接: https://www.lsjlt.com/news/186514.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python中的循环退出举例及while
    循环退出 for循环:forelsefor 循环如果正常结束,都会执行else语句。脚本1:    #!/usr/bin/env python    for i in xrange(10):        print i    else: ...
    99+
    2023-01-31
    Python
  • Python中退出While循环的三种方法举例
    Python中退出While循环的三种方法举例 在Python学习及编程应用中,常会使用while循环,对while循环条件设置不当可能导致进入死循环,本文将举例说明三种退出while循环的方法。 1...
    99+
    2023-09-11
    python while循环
  • python基础之while循环、for循环详解及举例
    目录1.while循环1.1Whlie循环的书写方式1.2while循环的格式1.3while循环注意事项1.4while嵌套的格式1.5while练习:计算 1~100 ...
    99+
    2022-11-10
  • python的while循环输出数字
    a. 使用while循环实现输出2-3+4-5+6...+100 的和 # 定义计算结果 aaa = '' bbb = 1 #for i in range(1, 100): i = 1 while i < 100: i +=...
    99+
    2023-01-31
    数字 python
  • python中的for循环对象和循环退出
    判断条件,1位true,0是flesh,成立时true,不成立flesh,not取反 if  1;      print 'hello python'    print 'true'   not取反,匹配取反,表示取非1大于2的正确...
    99+
    2023-01-31
    对象 python
  • python中的while循环
    1、死循环学会用法 a = 1 while True: print(a) a +=1 2、无限次输入,直到输对,才退出 _age = 18 while True: guess_age = int(input("...
    99+
    2023-01-31
    python
  • python中如何退出多层循环
    1、定义标记变量;利用变量值的变化退出循环   # 第一种嵌套形式 a = [[1, 2, 3], [5, 5, 6], [7, 8, 9]] # init_i = 0 # init_j = 0 flag = True for i in...
    99+
    2023-01-30
    多层 python
  • Python中的用for,while循环
    使用for循环遍历文件打开文件open     r:以读模式打开    w:以写模式打开    a:以追加模式打开    r+:以读写模式打开    w+:以读写模式打开(参见w)    a+:以读写模式打开(参见a)    rb:以二进制...
    99+
    2023-01-31
    Python
  • Python while 循环使用的简单实例
    while循环是在Python中的循环结构之一。 while循环继续,直到表达式变为假。表达的是一个逻辑表达式,必须返回一个true或false值,本文章向码农介绍Python while 循环使用方法,需...
    99+
    2022-06-04
    实例 简单 Python
  • 在Python的while循环中使用else以及循环嵌套的用法
    循环使用 else 语句 在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)...
    99+
    2022-06-04
    嵌套 Python
  • 如何在python函数中退出循环
    在python中退出循环的方法使用return语句退出循环def test():while True:for x in range(10):print(x)returntest()利用变量值的变化退出循环a = [[1, 2, 3], [5...
    99+
    2022-10-21
  • php中while()循环的示例分析
    这篇文章将为大家详细讲解有关php中while()循环的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。php有什么特点1、执行速度快。2、具有很好的开放性和可扩展性。3、PHP支持多种主流与非主流...
    99+
    2023-06-14
  • javascript中退出循环的方法
    这篇文章主要介绍“javascript中退出循环的方法”,在日常操作中,相信很多人在javascript中退出循环的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java...
    99+
    2022-10-19
  • 【Python入门篇】——Python中循环语句(while循环的嵌套应用,嵌套案例)
    作者简介: 辭七七,目前大一,正在学习C/C++,Java,Python等 作者主页: 七七的个人主页 文章收录专栏: Python入门,本专栏主要内容为Python的基础语法,Python中的选...
    99+
    2023-08-31
    python 编程语言 数据结构 算法
  • 如何使用Python中的while循环
    如何使用Python中的while循环在Python编程中,循环是非常重要的概念之一。循环可以帮助我们重复执行一段代码,直到满足指定条件为止。其中,while循环是一种被广泛使用的循环结构之一。通过使用while循环,我们可以根据条件的真假...
    99+
    2023-10-22
    Python编程 while循环 Python循环
  • js中几种循环的退出方式实例总结
    目录一、for循环二、forEach循环二、map循环三,for in 循环四,for of 循环五,every()和some()附:return、continue、break三者的...
    99+
    2022-12-08
    js循环退出方式是什么 js循环终止 js如何退出for
  • python中while循环的要素有哪些
    本篇文章给大家分享的是有关python中while循环的要素有哪些,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。python有哪些常用库python常用的库:1.requesu...
    99+
    2023-06-14
  • web前端开发中的while循环实例分析
    今天给大家介绍一下web前端开发中的while循环实例分析。文章的内容小编觉得不错,现在给大家分享一下,觉得有需要的朋友可以了解一下,希望对大家有所帮助,下面跟着小编的思路一起来阅读吧。while循环while(循环终止条件){}案例&nb...
    99+
    2023-06-05
  • Shell中的循环语句for、while、until实例讲解
    在编程语言中,循环语句是最基本的语法之一,在Shell(这里是Bash)中也不例外,再把以前自己写过的相关内容整理一下吧。这里包括for/while/until循环,以及变量自增的语法实例。 Shell(以...
    99+
    2022-06-04
    语句 实例 Shell
  • Shell中的while循环几种使用实例详解
    1.利用while循环计算1到100的和: 示例代码1: #!/bin/bash i=1 sum=0 while [ $i -le 100 ] do let sum=sum+$i let i++ done ech...
    99+
    2022-06-04
    shell中while循环 shell中while循环的使用 shell中的while循环几种使用实例详解
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作