python中如果在while迴圈中是return會導致迴圈中斷

來源:互聯網
上載者:User

標籤:python中如果在while迴圈中是return會導致迴圈中斷

python中如果在while迴圈中是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]4.5.223 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]#



本文出自 “心愿” 部落格,請務必保留此出處http://xinyuan8.blog.51cto.com/677906/1640339

python中如果在while迴圈中是return會導致迴圈中斷

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.