1 while?
while 主要用的場景沒有 for 循環多。 while循環:主要運行場景 我不知道什么時候結束。。。不知道運行多少次
1.1 基本用法
# while 4 > 3: #一直執行
# print("hell0")while 4 < 3: #不會打印,什么都沒有print("hell0")
while 循環的執行流程
#while 循環的執行流程
#當把while 循環下面的子分支執行完畢之后,
#程序會返回while 條件判斷語句。直至條件不滿足
#while趨勢時一個加強版的if, if判斷一次,while會回頭再判斷條件是否滿足
#可以用debug執行?
while 4 > 3:print("hell0")print("bye")print("no")
條件賦值?
#條件賦值,滿足什么條件后跳出循環
#while循環把for循環的機制手動化
cases = [{"url" : "http://", "method": "get"},{"url" : "http://examle", "method": "post"}
]index = 0
while index < len(cases):print(cases[index])#自動索引 + 1#需要縮進,不然不在循環中,會導致死循環index += 1--------結果---------
{'url': 'http://', 'method': 'get'}
{'url': 'http://examle', 'method': 'post'}
?1.2 break?
一般條件控制好,是不需要使用break的,但存在不好控制的條件。
cases = [{"url" : "http://", "method": "get"},{"url" : "http://examle", "method": "post"}
]index = 0
while True:print(cases[index])if index == 1:#手工終止,強制終止while循環或者forprint("索引為{},終止while循環".format(index))breakindex += 1-------------結果-------
{'url': 'http://', 'method': 'get'}
{'url': 'http://examle', 'method': 'post'}
索引為1,終止while循環
?1.3 pass
通常用在,子語句不做任何操作,用pass放在這作為一個占位符?,表示什么都不干
pass語法
while True:#無限跑abcpassprint("abc")if 1:#當有冒號有子語句的時候,目前還不知道這個語句怎么寫,先用pass占一個位置passprint("hell0")
elif 2:print("bey")
1.4? continue
?終止本次子語句
#continue 是表示跳過此次子與,進入下一個循環判斷
while True:continueprint("abc")
1.5 while循環的嵌套
while True:print("第一層")while True:print("the 第二層")