1. 程式人生 > 實用技巧 >學習Python流程控制之while迴圈、for迴圈Day5

學習Python流程控制之while迴圈、for迴圈Day5

迴圈結構
1、什麼是迴圈
迴圈就是重複做某件事

2、為何要有迴圈
為了控制計算機能夠像人一樣去重複做某件事

3、如何用迴圈



while迴圈基本語法:
python中有while與for兩種迴圈機制,其中while迴圈稱之為條件迴圈

while 條件:
程式碼1
程式碼2
程式碼3
while的執行步驟步驟1:如果條件為真,那麼依次執行:程式碼1、程式碼2、程式碼3......
步驟2:執行完畢後再次判斷條件,如果條件為True則再次執行:程式碼1
程式碼2、程式碼3......,如果條件為False,則迴圈終止

一、while迴圈基本用法
案例1:
# i=0
# tag=True

#
# while tag:
# if i == 5:
# tag=False
#
# print(i)
# i+=1
案例2:
# i = 0
# while i < 6:
# print(i)
# i += 1

# 0 # i=1
# 1 # i=2
# 2 # i=3
# 3 # i=4
# 4 # i=5
# 5 # i=6



二 死迴圈:條件永遠為True
# while True:
# # x = input(">>> ")
# # y = input(">>> ")
# # x + y
# 1+1




三 結束while迴圈的兩種方式

方式一: 把條件改為False
特點:等到本次迴圈體程式碼執行完畢後,下一次迴圈判斷條件時才會生效
# i=0
# tag=True
#
# while tag:
# if i == 5:
# tag=False
#
# print(i)
# i+=1

方式二:break代表結束本層迴圈
特點:立即幹掉本層while迴圈
# i=0
# tag=True
#
# while tag:
# if i == 5:
# tag=False
# # break
# print(i)
# i+=1




四、巢狀多層的while迴圈

方式一:while迴圈巢狀+tag的使用
針對巢狀多層的while迴圈,如果我們的目的很明確就是要在某一層直接退出所有層的迴圈,
其實有一個竅門,就讓所有while迴圈的條件都用同一個變數,該變數的初始值為True,一
旦在某一層將該變數的值改成False,則所有層的迴圈都結束

# tag = True
# while tag: # 第一層迴圈
# while tag: # 第二層迴圈
# while tag: # 第三層迴圈
# tag = False # tag變為False,所有while迴圈的條件都變為False


# 方式二:while迴圈巢狀+tag的使用
如果while迴圈嵌套了很多層,要想退出每一層迴圈則需要在每一層迴圈都有一個break

# while True: # 第一層迴圈
# while True: # 第二層迴圈
# while True: # 第三層迴圈
# break # 終止第三層迴圈
# break # 終止第二層迴圈
# break # 終止第一層迴圈


案例1:
# tag = True
# while tag:
# inp_user = input("username>>>: ")
# inp_pwd = input("password>>>: ")
#
# if inp_user == "egon" and inp_pwd == "123":
# print('login successful')
# # break
# tag = False
# else:
# print('username or password error')
#
# # print('=======================>')

案例2:
# while True:
# inp_user = input("username>>>: ")
# inp_pwd = input("password>>>: ")
#
# if inp_user == "egon" and inp_pwd == "123":
# print('login successful')
# while True:
# print("""
# 0 退出
# 1 取款
# 2 存款
# 3 轉賬
# """)
# choice = input("請輸入您的命令編號:")
# if choice == "0":
# break
# elif choice == "1":
# print("正在取款。。。")
# elif choice == "2":
# print("正在存款。。。")
# elif choice == "3":
# print("正在轉賬。。。")
# else:
# print("輸入的指令不存在")
# break
# else:
# print('username or password error')





五:while+continue:結束本次迴圈,直接進入下一次迴圈
# count = 0
# while count < 6:
# if count == 2 or count == 4:
# # break
# count += 1
# continue # continue同級別之後千萬別寫程式碼,寫了也不會執行
# print(count)
# count += 1

案例:
# while True:
# inp_user = input("username>>>: ")
# inp_pwd = input("password>>>: ")
#
# if inp_user == "egon" and inp_pwd == "123":
# print('login successful')
# break
# else:
# print('username or password error')
# # continue # continue一定不要加在最後一步






六:while+else:else的子程式碼塊會在while迴圈正常死亡時執行,沒有被break幹掉就叫正常死亡
案例一:正常死亡,迴圈正常執行
# count = 0
# while count < 5:
# if count == 2:
# count += 1
# continue
# print(count)
# count += 1
# else:
# print('====>')

案例二:非正常死亡,如果執行過程中被break,就不會執行else的語句
# count = 0
# while count < 5:
# if count == 2:
# break
# print(count)
# count += 1
# else:
# print('====>')





for迴圈基本語法
迴圈結構的第二種實現方式是for迴圈,for迴圈可以做的事情while迴圈都可以實現,
之所以用for迴圈是因為在迴圈取值(即遍歷值)時for迴圈比while迴圈的使用更為簡潔

for 變數名 in 可迭代物件: # 此時只需知道可迭代物件可以是字串\列表\字典,我們之後會專門講解可迭代物件
    程式碼一
    程式碼二
    ...

 一、for迴圈的基本用:
案例1:遍歷列表
# names = ["egon", "tom", "jack", "lili", "lxx", "xxx"]
# for x in names:
# print(x)

# for x,y in [["name","egon"],["age",18],["gender","male"]]:  # x,y = ['name', 'egon']
# print(x,y)


案例2:遍歷字典

# dic={"k1":111,'K2':2222,"k3":333}
# for x in dic: # for 迴圈預設取的是字典的key賦值給變數名k
# print(x,dic[x])
案例3:遍歷字串
# for x in "hello":
# print(x)



二、for+break
# for i in [11,22,33,44,55]:
# if i == 33:
# break
# print(i)



三、for+continue
# for i in [11,22,33,44,55]:
# if i == 33:
# continue
# print(i)



四、for+else
# for i in [11,22,33,44,55]:
# if i == 33:
# break
# print(i)
# else:
# print('++++++++>')



五、range()
# i=0
# while i < 3:
# print(111)
# print(222)
# print(333)
# i+=1

# for x in range(3):
# print(111)
# print(222)
# print(333)