python中如果在while循環中return會導致循環中斷

python中如果在while循環中直接return會導致循環中斷,可以賦予變量再return 變量方式


[[email protected] 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()
[[email protected] root]#

[[email protected] 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
[[email protected] root]#



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章