码迷,mamicode.com
首页 > 编程语言 > 详细

python中如果在while循环中是return会导致循环中断

时间:2015-04-29 17:32:27      阅读:817      评论:0      收藏:0      [点我收藏+]

标签:python中如果在while循环中是return会导致循环中断

python中如果在while循环中是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

python中如果在while循环中是return会导致循环中断

标签:python中如果在while循环中是return会导致循环中断

原文地址:http://xinyuan8.blog.51cto.com/677906/1640339

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!