美文网首页
退出循环和while

退出循环和while

作者: C1awn_ | 来源:发表于2017-12-27 22:07 被阅读0次

1. 循环退出

  • break,跳出整个循环
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
for i in xrange(10):
    if i == 5:
        break
    print i
else :
    print "end"

对于上述程序,因为有break,后续语句不再执行,结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
0
1
2
3
4

Process finished with exit code 0
  • continue,跳出当前的小循环
    把上面的代码加几行:
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
for i in xrange(10):
    if i == 3:
        continue
    if i == 5:
        break
    print i
else :
    print "end"

结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
0
1
2
4

Process finished with exit code 0
  • pass占位,啥也不做
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
for i in xrange(10):
    if i == 3:
        continue
    if i == 5:
        continue
    if i == 6:
        pass
    print i,
else :
    print "end",
print "haha"

这样的话3和5都不会打印,可以看下结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
0 1 2 4 6 7 8 9 end haha

Process finished with exit code 0
  • sys.exit()退出程序,使用时要先插入sys模块
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
import sys
for i in xrange(10):
    if i == 3:
        continue
    if i == 5:
        continue
    if i == 6:
        sys.exit()
    print i,
else :
    print "end",
print "haha"

结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
0 1 2 4

Process finished with exit code 0
  • 猜数字游戏

系统生成一个20以内的随机整数,
玩家有6次机会进行猜猜看,每次猜测都有反馈(猜大了, 猜小了,猜对了-结束)
6次中,猜对了,玩家赢了。
否则系统赢了。

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
import random
import sys
random_num = random.randint(1, 20)
for i in xrange(1, 7):
    enter_num = int(raw_input('Please input a num between 1 and 20:  '))
    if random_num < enter_num:
        print 'the enter_num is bigger.'
    elif random_num > enter_num:
        print 'the enter_num is lower.'
    elif random_num == enter_num:
        print 'you are win'
        sys.exit()
print 'game over'

结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171228/猜数字.py
The random have been created
Please input a num between 1 and 20:  5
the enter_num is lower.
Please input a num between 1 and 20:  6
the enter_num is lower.
Please input a num between 1 and 20:  7
the enter_num is lower.
Please input a num between 1 and 20:  8
the enter_num is lower.
Please input a num between 1 and 20:  9
the enter_num is lower.
Please input a num between 1 and 20:  10
the enter_num is lower.
game over

Process finished with exit code 0

2. 流程控制--while

  • while与for相比
    • for循环用在有次数的循环上。
    • while循环用在有条件的控制上。
  • whle循环,直到表达式变为假,才退出while循环,表达式是一个逻辑表达式,必须返回一个True或False。
  • 语法:
    while expression:
    statement(s)
  • while很容易写成死循环,造成负载过大
    比如下面的脚本:
#!/usr/bin/python
while 1:
    print "hello"

如果执行,计算机会不停输出hello。

  • 修改版本1:
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
n = 0
while 1:
    if n == 5:
         break
    print n,"hello"
    n += 1

输出结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
0 hello
1 hello
2 hello
3 hello
4 hello

Process finished with exit code 0
  • 修改版本2:等待键盘输入
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
while 1:
    print "hello"
    if raw_input("please input sth,q for quit:  ") == "q":
        break

执行结果

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
hello
please input sth,q for quit:  a
hello
please input sth,q for quit:  q

Process finished with exit code 0
  • 修改版本3:while条件是false,退出循环
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
n = "" #n最开始为空
while n != "q" :
    print "hello"
    n = raw_input("please input sth,q for quit:  ")

结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
hello
please input sth,q for quit:  fgg
hello
please input sth,q for quit:  q

Process finished with exit code 0
  • while里的else,类比其他循环
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
n = ""
while n != "q" :  #第一次n为空,所以继续下面的语句
    print "hello"
    n = raw_input("please input sth,q for quit:  ")
    if n == "q":  #如果输入q,跳出while循环,执行else语句
        continue
else:
    print "world"

结果:

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/171226/ts.py
hello
please input sth,q for quit:  w
hello
please input sth,q for quit:  q
world

Process finished with exit code 0

3. 练习

习题

  1. 将一个正整数分解质因数。例如:输入90,打印出90=233*5。
    程序分析:对n进行分解质因数,应先找到一个最小的质数i,然后按下述步骤完成:
    (1)如果分解后商为1,则说明分解质因数的过程已经结束,打印出即可。
    (2)如果商不为1,则应打印出i的值,并用n除以i的商,作为新的正整数进行分解,
     重复执行第一步。
    (3)如果n不能被i整除,则i的值加1,重复执行第一步。
  1. 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
    程序分析:采取逆向思维的方法,从后往前推断。
  1. 解答:
def fun(n):

    for i in range(2, n / 2 + 1):

        if n % i == 0:

            print(i),

            print("*"),

            return fun(n / i)

    print(n),



if __name__ == "__main__":

    i = int(input("输入数的数为: "))

    fun(i)
  1. 解答
num = 1

for i in range(9, 0, -1):

    num = (num + 1) * 2

print('第一天共摘了%s个桃子' %num)

相关文章

  • 退出循环和while

    1. 循环退出 break,跳出整个循环 对于上述程序,因为有break,后续语句不再执行,结果: continu...

  • 在while循环中,break,continue,return有

    break--退出while循环,但while循环后面还有其他语句的话,还是会执行, continue--退出当次...

  • 2018.01.09

    控制流 if、elif和else for循环 break关键字用于使for循环完全退出 while循环 只要条件不...

  • python中退出for循环的两种方式:break 和conti

    其实break和continue退出for循环的用法和退出while的用法是一样的。break,当某些条件成立退出...

  • 循环语句

    while循环 while(条件表达式){ //循环体 } 条件表达式为真继续循环,为假退出循环。 break...

  • shell的for循环

    最佳推荐While 适合文件逐行处理For 固定循环While until不固定循环(需要满足条件退出) For...

  • 42 awk 控制流

    if while for break 当 break 语句用于 while 或 for 语句时,导致退出程序循环。...

  • Js流程控制语句(二)循环语句

    主要有for,while, while break, 彻底退出循环continue :结束本次循环,继续开始下一次...

  • C语言流程控制-----while

    while循环结构 格式while(条件表达式){语句}构成循环结构的几个条件循环控制条件循环退出的主要依据,来控...

  • 【Python】while循环

    ctrl+c结束死循环 break 退出循环 while+if判断+break中断循环求1-100的整数和 con...

网友评论

      本文标题:退出循环和while

      本文链接:https://www.haomeiwen.com/subject/ayhpgxtx.html