网上电玩

IT培訓網| | | | | | | |
|
400-650-7353

精品課程

您所在的位置:首頁 > IT干貨資料 > python > 【Python基礎知識】Python匯總的while語句怎么使用

【Python基礎知識】Python匯總的while語句怎么使用

1、while循環語句

diedai(iteration)yiweizhufanfuzhixingxiangtongdedaimakuai。shixiandiedaidebianchengjiegouchengweixunhuan(loop)。

网上电玩jiasheyouyixiangrenwu,yaozaipingmushangshuchucong1dao100deshuzi。jinjinshiyongzhiqiantaolunguodezhishi,kenenghuixiechuruxiadaima:

  1. print(1
  2. print(2
  3. print(3
  4. # 此處省略print(4) ~ print(99) 
  5. print(100

zheyangzuofeichangmafan,yebucongming。xuehuibianchengdehaochuzhiyijiushikeyifangbiandichulizhongfugongzuo。pythonzhongdexunhuankeyifenweiwhilexunhuanheforxunhuanliangzhong。

yuifyujuleisi,whileyujutongyangshouxianpanduanbuerbiaodashi,ruguoweizhen,neimejixuzhixingxunhuanti;ruguoweijia,neimetiaochuxunhuanti。whilexunhuandeyufageshiruxia:

while biaodashi:

xunhuanti

shiyongwhilexunhuanlaiwanchengcong1dao100deshuchurenwu:

  1. >>> n = 1   # 因為從1開始打印,所以先將n賦值為1 
  2. >>> while n <= 100:   # 每次都判斷n的值是否小于等于100 
  3. ...     print(n)   # 打印n的值 
  4. ...     n = n + 1   # 每次將n的值增加1,以便打印2、3、4…… 
  5. ...  
  6. 1 
  7. 2 
  8. 3 
  9. 4 
  10. …… 
  11. 98 
  12. 99 
  13. 100 

whilexunhuanzongshibuduandipanduantiaojianshifoumanzu,zhidaotiaojianbumanzucaitiaochuxunhuan。liyongzhegetexing,keyibianxieyige“caishuzi”deyouxi。xianyouyigerenshuruyigeshuzi(ziranshu),zhegeshuzuoweimidi。zaiyoulingwaiyigerencai,ruguocaideshuzibimidida,neimedayin“caidale”; ruguocaideshuzibimidixiao,neimedayin“caixiaole”;zhidaocaideshuziyumidixiangdengweizhi,dayin“gongxi,caiduile!”。daimashiliruxia:

  1. answer = int(input('請輸入謎底數字:')) 
  2. number = int(input('請輸入您要猜的數字:')) 
  3. while number != answer:   # 如果number不等于answer,即猜錯了,那么進入循環 
  4.     if number > answer:   # 如果猜的數字大于謎底 
  5.         print('猜大了!'
  6.     else:   # 如果猜的數字小于謎底 
  7.         print('猜小了!'
  8.     number = int(input('請繼續輸入您要猜的數字:')) 
  9. print('恭喜,猜對了!')   # 如果跳出循環,那么說明猜對了 

网上电玩jianyijiangzhegechengxushiyongjiaobenmoshiyunxing,shurushuju,ceshiyunxingjieguo:

  1. 請輸入謎底數字:77 
  2. 請輸入您要猜的數字:20 
  3. 猜小了! 
  4. 請繼續輸入您要猜的數字:90 
  5. 猜大了! 
  6. 請繼續輸入您要猜的數字:80 
  7. 猜大了! 
  8. 請繼續輸入您要猜的數字:77 
  9. 恭喜,猜對了! 

2、小心死循環

网上电玩sixunhuanshizhichengxuzailiuchengkongzhizhongyizhizhongfuyunxingmouyiduandaima,wufayikaozishendekongzhizhongzhixunhuan。chuxuezhehenrongyibuxiaoxinxiechusixunhuan。liru,zaiqianwenshiyongwhilexunhuanwanchengcong1dao100deshuchurenwuzhong,ruguojiangzuihouyixingdaima“n = n + 1”shandiao,neimegaixunhuanjiubianchenglesixunhuan。zheshiyinweindezhibuhuizengjia,congerhuiyizhimanzu“n <= 100”detiaojianwufatiaochuxunhuan。shizhuyunxingruxiadaima:

  1. >>> n = 1 
  2. >>> while n < 100
  3. ...     print(n) 
  4. ...     
  5. 1 
  6. 1 
  7. 1 
  8. 1 
  9. 1 
  10. 1 
  11. 1 
  12. 1 
  13. …… 

网上电玩dangqiaoxiazuihouyigehuichejianshi,chengxuhuizaipingmushangbutingdidayin1,zhidaoqiangzhiguanbichengxuchuangkou,huozheanctrl+cjianzhongzhichengxu。sixunhuankenenghuishidiannaobengkuihuozhezaochengqitayuyuqibufudehouguo,yinci,zaishiyongwhilexunhuanshiyaogewaixiaoxin,kankanshifouyounenggoumanzutiaojiantiaochuxunhuande“chukou”。

xuyaozhichudeshi,youxieyingyongchangjingxiaxuyaoshiyongsixunhuan(liru,caozuoxitongzuiwaicengshisixunhuan,baozhengdiannaoyizhiyunxing),yejiushishuoxiechusixunhuanbuyidingshicuode,danyinggaijindangqingchuzijizaizuoshenmeshi,caixiesixunhuan。

課程好禮申請領取

网上电玩网上电玩游戏

官方微信

掃描上方二維碼或者手動搜索ujiuye,關注【网上电玩 】,每日推送行業資訊、干貨資料、熱門話題、招聘信息和求職攻略,還有不定期的優惠活動一手發布!

>>本文地址:
网上电玩 注:本站稿件未經許可不得轉載,轉載請保留出處及源文件地址。

相關推薦

趁年輕 學門技術充個電
0元
入學
30天免費試學, 3000
直減優惠
立即
搶占
2周
特訓
價值299的特訓營課程,
9.9元搶購
立即
搶購
80G
資源包
IT行業11類熱門崗位全套資源包
立即
領取
課程資料、活動優惠 領取通道
 
 
  • 返回頂部
电玩_电玩捕鱼_电玩捕鱼城 电玩捕鱼_电玩捕鱼游戏_电玩城捕鱼游戏 电玩城捕鱼_电玩城游戏_电玩捕鱼游戏 电玩游戏大厅 电玩游戏大厅