Python——while、break、continue、else

來源:互聯網
上載者:User

標籤:continue   偶數   完全   done   迴圈   沒有   big   input   中間   

1.while迴圈語句:

1  count = 02 while count <= 100:3      print("loop ", count)4      count += 15 print(‘----loop is ended----‘)

2.列印偶數:

 1 # 列印偶數 2 # count = 0 3 # 4 # while count <= 100: 5 # 6 #     if count % 2 == 0: #偶數 7 #         print("loop ", count) 8 # 9 #     count += 110 #11 #12 # print(‘----loop is ended----‘)

3.第50次不列印,第60-80列印對應值 的平方

 1 count = 0 2  3 while count <= 100: 4  5     if count == 50: 6         pass #就是過。。 7  8     elif count >= 60 and count <= 80: 9         print(count*count)10 11     else:12         print(‘loop ‘, count)

count+=1

4.死迴圈

1 count = 02 3 while True:4     print("forever 21 ",count)5     count += 1

5.迴圈終止語句:break&continue

break:用於完全結束一個迴圈,跳出迴圈體執行迴圈後面的語句

continue:不會跳出整個迴圈,終止本次迴圈,接著執行下次迴圈,break終止整個迴圈

 

1 # break 迴圈2 # count=03 # while count<=100:4 #     print(‘loop‘,count)5 #     if count==5:6 #         break7 #     count+=18 # print(‘-----out of while loop---‘)

 

1 #continue 迴圈2 count=03 while count<=100:4     print(‘loop‘,count)5     if count==5:6         continue7     count+=18 # print(‘-----out of while loop---‘)   #一直列印59 #continue 跳出本次迴圈,不會執行count+=1,count一直為5

例1:讓使用者猜年齡

1 age = 262 user_guess = int(input("your guess:"))3 if user_guess == age :4     print("恭喜你答對了,可以抱得傻姑娘回家!")5 elif user_guess < age :6     print("try bigger")7 else :8     print("try smaller")

例2:使用者猜年齡升級版,讓使用者猜年齡,最多猜3次,中間猜對退出迴圈

 1 count = 0 2 age = 26 3  4 while count < 3: 5  6     user_guess = int(input("your guess:")) 7     if user_guess == age : 8         print("恭喜你答對了,可以抱得傻姑娘回家!") 9         break10     elif user_guess < age :11         print("try bigger")12     else :13         print("try smaller")14 15     count += 1

例3:使用者猜年齡繼續升級,讓使用者猜年齡,猜3次,如果使用者3次都錯,詢問使用者是否還想玩,如果為y,則繼續猜3次,以此往複【註解提示:相當於在count=3的時候詢問使用者,如果還想玩,等於把count變為0再從頭跑一遍】

count = 0age = 26while count < 3:    user_guess = int(input("your guess:"))    if user_guess == age :        print("恭喜你答對了,可以抱得傻姑娘回家!")        break    elif user_guess < age :        print("try bigger")    else :        print("try smaller")    count += 1    if count == 3:        choice = input("你個笨蛋,沒猜對,還想繼續麼?(y|Y)")        if choice == ‘y‘ or choice == ‘Y‘:            count = 0

6.while&else:while 後面的else的作用是,當while迴圈正常執行完,中間沒有被break中止的話,就會執行else後的語句【else 檢測你的迴圈過程有沒有中止過(當你的迴圈代碼比較長的時候)】

 

1  count=02 # while count <=5:3 #     count+=14 #     print(‘loop‘,count)5 #6 # else:7 #     print(‘迴圈正常執行完啦‘)8 # print(‘-----out of while loop----‘)
1 count=02 while count<=5:3     print(‘loop‘,count)4     if count==3:5         break6     count+=17 else:8     print(‘loop is done‘)

 

Python——while、break、continue、else

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.