溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

條件控制之while和for

發(fā)布時(shí)間:2020-07-28 11:33:04 來源:網(wǎng)絡(luò) 閱讀:155 作者:okada88 欄目:編程語言

一。while 循環(huán)
     1.循環(huán):重復(fù)做某件事

    2.語法
         while  條件:
         code1

    3.結(jié)束while的方式:
         1.條件不滿足,下次循環(huán)開始時(shí)判斷
         2.break直接結(jié)束本層循環(huán)

    4.while + continue
         continue 之后的代碼不會(huì)運(yùn)行了,直接開始下次循環(huán)
n= 0
while n < 6:
     if n == 4:
         n+=1
         continue
     else:
         print(n)
     n+=1

       
     5.while循環(huán)嵌套
         while:
             while:
                 while:
        

        break
             break
                 break
                
                
                
                
                
count = 1
tag = True
while tag:
     name = input("name: ")
     passwd = input("passwd ")
     if count == 3:
         print("too many")
         break
     if name == "chad" and passwd == "123":
         print("successfull")
         while tag:
             print("""
             1
             2
             3
             """)
             cho = input("choice:")
             if cho == "1":
                 print(1)
             elif cho == "2":
                 print(2)
             else:
                 print(3)
                 tag = False
     else:
         print("error")
         count +=1
                
    
    
     6. while + else
         如果while循環(huán)沒有被break打斷,才會(huì)執(zhí)行
         循環(huán)要正常結(jié)束
count = 0
while count < 3:
     print(count)
     count += 1
else:
     print("run")   

二。for   循環(huán)
     循環(huán)取值簡潔
         for+brek
         for+continue
         for+else

    dic = {"name":"sdfa","age":23}
     for i in dic:
         print(i,dic[i])
     返回字符串

    dic = {"name":"sdfa","age":23}
     for i,r in dic.items():
         print(i,r)
     以字符串返回key和value  

    dic = {"name":"sdfa","age":23}
     for i in dic.items():
         print(i)
     以元組返回key和value,一對(duì)key,value是一個(gè)元組

range(起始,結(jié)束,步長)
取頭不取尾

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI