99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python While循環(huán)語(yǔ)句_python基礎(chǔ)教程
Python While循環(huán)語(yǔ)句_python基礎(chǔ)教程
2016-12-31
收藏

Python While循環(huán)語(yǔ)句_python基礎(chǔ)教程

Python編程中 while 語(yǔ)句用于循環(huán)執(zhí)行程序,即在某條件下,循環(huán)執(zhí)行某段程序,以處理需要重復(fù)處理的相同任務(wù)。其基本形式為:

while判斷條件:執(zhí)行語(yǔ)句……

執(zhí)行語(yǔ)句可以是單個(gè)語(yǔ)句或語(yǔ)句塊。判斷條件可以是任何表達(dá)式,任何非零、或非空(null)的值均為true。

當(dāng)判斷條件假false時(shí),循環(huán)結(jié)束。

執(zhí)行流程圖如下:

實(shí)例:

#!/usr/bin/pythoncount=0while(count<9):print'The count is:',count count=count+1print"Good bye!"

以上代碼執(zhí)行輸出結(jié)果:

Thecountis:0Thecountis:1Thecountis:2Thecountis:3Thecountis:4Thecountis:5Thecountis:6Thecountis:7Thecountis:8Goodbye!

while 語(yǔ)句時(shí)還有另外兩個(gè)重要的命令 continue,break 來(lái)跳過(guò)循環(huán),continue 用于跳過(guò)該次循環(huán),break 則是用于退出循環(huán),此外”判斷條件”還可以是個(gè)常值,表示循環(huán)必定成立,具體用法如下:

# continue 和 break 用法i=1whilei<10:i+=1ifi%2>0:# 非雙數(shù)時(shí)跳過(guò)輸出continueprinti# 輸出雙數(shù)2、4、6、8、10i=1while1:# 循環(huán)條件為1必定成立printi# 輸出1~10i+=1ifi>10:# 當(dāng)i大于10時(shí)跳出循環(huán)break



無(wú)限循環(huán)

如果條件判斷語(yǔ)句永遠(yuǎn)為 true,循環(huán)將會(huì)無(wú)限的執(zhí)行下去,如下實(shí)例:

#!/usr/bin/python# -*- coding: UTF-8 -*-var=1whilevar==1:# 該條件永遠(yuǎn)為true,循環(huán)將無(wú)限執(zhí)行下去num=raw_input("Enter a number :")print"You entered: ",numprint"Good bye!"

以上實(shí)例輸出結(jié)果:

Entera number:20Youentered:20Entera number:29Youentered:29Entera number:3Youentered:3Entera number between:Traceback(most recent calllast):File"test.py",line5,in<module>num=raw_input("Enter a number :")KeyboardInterrupt

注意:以上的無(wú)限循環(huán)你可以使用 CTRL+C 來(lái)中斷循環(huán)。



循環(huán)使用 else 語(yǔ)句

python中,for … else 表示這樣的意思,for 中的語(yǔ)句和普通的沒(méi)有區(qū)別,else 中的語(yǔ)句會(huì)在循環(huán)正常執(zhí)行完(即 for 不是通過(guò) break 跳出而中斷的)的情況下執(zhí)行,while … else 也是一樣。

#!/usr/bin/pythoncount=0whilecount<5:printcount," is less than 5"count=count+1else:printcount," is not less than 5"

以上實(shí)例輸出結(jié)果為:

0isless than51isless than52isless than53isless than54isless than55isnotless than5



簡(jiǎn)單語(yǔ)句組

類(lèi)似if語(yǔ)句的語(yǔ)法,如果你的while循環(huán)體中只有一條語(yǔ)句,你可以將該語(yǔ)句與while寫(xiě)在同一行中, 如下所示:

#!/usr/bin/pythonflag=1while(flag):print'Given flag is really true!'print"Good bye!"

注意:以上的無(wú)限循環(huán)你可以使用 CTRL+C 來(lái)中斷循環(huán)。


數(shù)據(jù)分析咨詢(xún)請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢(xún)
客服在線
立即咨詢(xún)
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }