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