首页 > 代码库 > 【python之路4】循环语句

【python之路4】循环语句

1、while 循环语句

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time
bol = True
while bol:
    print ‘1‘
    time.sleep(1)
    bol = False

print ‘hello,world!‘

2、无限的输出数字

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time
n = 0
while True:
    n = n + 1
    time.sleep(1)
    print n

3、打印输出10个数字

#!/usr/bin/env python
# -*- coding:utf-8 -*-
bol = True
n = 0
while bol:
	n = n + 1
	if n == 10:
		bol = False
	print n
print "end"

 4、break退出本循环语句继续向下运行

#!/usr/bin/env python
# -*- coding:utf-8 -*-
n = 0
while True:
	n = n + 1
	print n
	if n == 10:
		break

print "end"

 

【python之路4】循环语句