python中如果在while循环中return会导致循环中断
2015-04-29 15:25
411 查看
python中如果在while循环中直接return会导致循环中断,可以赋予变量再return 变量方式
[root@10.144.5.223 root]# cat test_while_return.py
count = 0
while (count < 6):
print 'The count is:', count
count = count + 1
print "Good bye!"
print '-'*20
while (count >= 3):
print 'The count is:', count
count -= 1
print "Good bye!"
print '-'*20
while (count != 0):
print 'The count is:', count
count -= 1
print "Good bye!"
print '#'*20
def checkstatus():
Checktimes = 3
Mysql_ok = 1
while (Checktimes != 0):
Checktimes -= 1
if Mysql_ok == 1:
print ('Mysql_ok',Mysql_ok)
return True
#exit( 0 )
else:
print ('Mysql_ok',dbip,Mysql_ok)
return False
#exit( 1 )
checkstatus()
print '*'*20
def checkstatus():
Checktimes = 3
Mysql_ok = True
while (Checktimes != 0):
Checktimes -= 1
if Mysql_ok == 1:
print ('Mysql_ok',Mysql_ok)
mysqlcheck=True
#exit( 0 )
else:
print ('Mysql_ok',dbip,Mysql_ok)
mysqlcheck=False
#exit( 1 )
return mysqlcheck
checkstatus()
print '+'*20
print checkstatus()
[root@10.144.5.223 root]#
[root@10.144.5.223 root]# python test_while_return.py
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
Good bye!
--------------------
The count is: 6
The count is: 5
The count is: 4
The count is: 3
Good bye!
--------------------
The count is: 2
The count is: 1
Good bye!
####################
('Mysql_ok', 1) #这里被中断只循环了一次
********************
('Mysql_ok', True)
('Mysql_ok', True)
('Mysql_ok', True)
++++++++++++++++++++
('Mysql_ok', True)
('Mysql_ok', True)
('Mysql_ok', True)
True
[root@10.144.5.223 root]#
本文出自 “心愿” 博客,请务必保留此出处http://xinyuan8.blog.51cto.com/677906/1640339
[root@10.144.5.223 root]# cat test_while_return.py
count = 0
while (count < 6):
print 'The count is:', count
count = count + 1
print "Good bye!"
print '-'*20
while (count >= 3):
print 'The count is:', count
count -= 1
print "Good bye!"
print '-'*20
while (count != 0):
print 'The count is:', count
count -= 1
print "Good bye!"
print '#'*20
def checkstatus():
Checktimes = 3
Mysql_ok = 1
while (Checktimes != 0):
Checktimes -= 1
if Mysql_ok == 1:
print ('Mysql_ok',Mysql_ok)
return True
#exit( 0 )
else:
print ('Mysql_ok',dbip,Mysql_ok)
return False
#exit( 1 )
checkstatus()
print '*'*20
def checkstatus():
Checktimes = 3
Mysql_ok = True
while (Checktimes != 0):
Checktimes -= 1
if Mysql_ok == 1:
print ('Mysql_ok',Mysql_ok)
mysqlcheck=True
#exit( 0 )
else:
print ('Mysql_ok',dbip,Mysql_ok)
mysqlcheck=False
#exit( 1 )
return mysqlcheck
checkstatus()
print '+'*20
print checkstatus()
[root@10.144.5.223 root]#
[root@10.144.5.223 root]# python test_while_return.py
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
Good bye!
--------------------
The count is: 6
The count is: 5
The count is: 4
The count is: 3
Good bye!
--------------------
The count is: 2
The count is: 1
Good bye!
####################
('Mysql_ok', 1) #这里被中断只循环了一次
********************
('Mysql_ok', True)
('Mysql_ok', True)
('Mysql_ok', True)
++++++++++++++++++++
('Mysql_ok', True)
('Mysql_ok', True)
('Mysql_ok', True)
True
[root@10.144.5.223 root]#
本文出自 “心愿” 博客,请务必保留此出处http://xinyuan8.blog.51cto.com/677906/1640339
相关文章推荐
- python的一个函数里面有while,在while里面执行了return,是跳出整个循环,还是跳出整个函数
- python中while循环语句用法简单实例
- Python 初探3 条件分支与while循环,逻辑操作
- Python流程控制的 for、 while、循环和嵌套词汇continue、break、range
- Python之if,for和while循环
- 笨方法学习Python-习题33: While 循环
- Python学习笔记03 for循环和while循环
- 20180403-Python if条件判断及while循环
- Python笔记-if语句、for循环、while循环
- python中循环语句while用法实例
- 3.4Python流程控制(4):while循环
- 第33个python程序:while循环
- 在Python的while循环中使用else以及循环嵌套的用法
- Python循环语句for和while
- 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。
- 【python】理解循环:for,while
- while循环的中断
- Python -- 5. 用户输入和while 循环
- Python while 循环使用实例
- python-----运算符及while循环