條件控制之while和for

2021-09-29 08:33:32 字數 1501 閱讀 6336

一。while 迴圈

1.迴圈:重複做某件事

2.語法

while  條件:

code1

3.結束while的方式:

1.條件不滿足,下次迴圈開始時判斷

2.break直接結束本層迴圈

4.while + continue

continue 之後的**不會執行了,直接開始下次迴圈

n= 0

while n < 6:

if n == 4:

n+=1

continue

else:

print(n)

n+=1

5.while迴圈巢狀

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("""12

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迴圈沒有被break打斷,才會執行

迴圈要正常結束

count = 0

while count < 3:

print(count)

count += 1

else:

print("run")    

二。for   迴圈

迴圈取值簡潔

for+brek

for+continue

for+else

dic =

for i in dic:

print(i,dic[i])

返回字串

dic =

for i,r in dic.items():

print(i,r)

以字串返回key和value   

dic =

for i in dic.items():

print(i)

以元組返回key和value,一對key,value是乙個元組

range(起始,結束,步長)

取頭不取尾

python之if和while控制流

python之if和while控制流 1 usr bin env python2 coding utf 8 34 name raw input 請輸入你的名字 5 age input 請輸入你的年齡 6 job raw input 請輸入你的工作 7 salary input 請輸入你的工資 89p...

while條件語句

while條件語句 條件滿足一直迴圈,適合做守護程序,死迴圈,while是讀整行,for遇見空格就另起一行 語法 while 條件 do指令 done 案例 每個2秒記錄一次系統負載情況 while true do 也可以寫成 while do bin bash while true do upti...

Python學習 條件控制語句 while迴圈

if 條件表示式 語句塊 塊 判斷a是不是乙個整數a 6 if a 0 print a,是乙個正數 其執行結果為 6 是乙個正數 if 條件表示式 塊1 else 塊2 比較a和b的值的大小a 1 b 2if a b print a的值大於b的值 else print a的值小於b的值 其執行結果為...