1.練習 需求:三級菜單,輸入目錄地址進入下一級菜單 代碼: area={ 'hubei':{'huanggang':['qichun','wuxue','huangzhou'], 'wuhan':['wuchang','hankou','hongshan'] }, 'jiangsu':{'nanj ...
1.練習
需求:三級菜單,輸入目錄地址進入下一級菜單
代碼:

area={ 'hubei':{'huanggang':['qichun','wuxue','huangzhou'], 'wuhan':['wuchang','hankou','hongshan'] }, 'jiangsu':{'nanjing':['jianye','baixia','gulou'], 'suzhou':['wuzhong','sugu','xiangcheng'] } } floor=area empty_list=[] while True: for key in floor: print(key) choice=input("請輸入地址【返回/b,退出/q】:") if choice=='q': break if len(choice)==0: continue if choice in floor: #如果輸入的地址是當前列表的值 empty_list.append(floor) #先將當前列表加入空列表empty_list中中 floor=floor[choice] elif choice =='b': if empty_list: floor=empty_list.pop() else: print("您輸入的有誤!")View Code
運行結果:

hubei
jiangsu
請輸入地址【返回/b,退出/q】:
View Code
2.練習
需求:登陸認證
代碼:

username="wang" password="wang@123" for i in range(3): _username=input("Please enter your name: ") _password=input("please enter your PIN:") if username==_username and password==_password: print("Welcome to log!") break else: print("You entered an incorrect user name or password") print("Yours is temporarily locked!")View Code
3.字元編碼轉換
代碼:

msg="我愛中國" print(msg) print(msg.encode(encoding="utf-8"))#將字元串轉為bytes類型 print(msg.encode(encoding="utf-8").decode(encoding="utf-8"))#將bytes類型轉為字元串類型View Code
4.數組處理
代碼:

num=[10,11,12,13,14] print(num) num.append(15)#append方法給數組後面加元素 print(num) num.insert(0,9)#insert方法給數組指定位置加元素 print(num) num.remove(15)#刪除數組中元素15 print(num) del num[0]#刪除第一個元素 print(num) num.pop(0)#刪除第一個元素 print(num)View Code
5.os模塊
代碼:

import os #cmd_res=os.system("dir") #執行命令 不保存結果 cmd_res=os.popen("dir").read() print("-->",cmd_res) os.mkdir("new_dir")#新建目錄View Code
輸出結果:

--> 驅動器 E 中的捲是 新加捲 捲的序列號是 083D-2CEF E:\python\day2 的目錄 2020/05/14 15:17 <DIR> . 2020/05/14 15:17 <DIR> .. 2020/05/13 14:44 373 log.py 2020/05/13 17:53 822 menu.py 2020/05/13 22:22 195 msg.py 2020/05/13 18:00 <DIR> new_dir 2020/05/13 22:51 313 num.py 2020/05/13 18:04 156 sys_mod.py 2020/05/14 15:17 1,779 test.py 6 個文件 3,638 位元組 3 個目錄 199,113,728,000 可用位元組View Code
6.練習:
需求:
(1)購物車程式,輸入工資,選擇購買的商品
(2)購買結束列印購買商品列表及餘額
代碼:

product_list=[ ('Iphone',5000), ('Watch',2000), ('Ipad',3000), ('Computer',6000), ('book',50) ] shooping_cart=[] #定義購物車 salary=(input("請輸入您的工資:")) while True: if salary.isdigit():#判斷輸入的內容是否為數字 salary=int(salary) #將輸入的數字轉為整數型 break else: print("請輸入正確的數字") salary=(input("請輸入您的工資:")) while True: for index,item in enumerate(product_list):#可用enumerate方法獲取元素索引 print(index,item) #print(product_list.index(item),item)#或者直接列印索引 user_choice=input("請選擇您要購買的商品序號(或輸入q退出):") if user_choice.isdigit():#判斷輸入的內容是否為數字 user_choice=int(user_choice)#將輸入的數字轉為整數型 if user_choice<len(product_list) and user_choice>=0:#判斷輸入的數字是否在合理範圍內 p_item=product_list[user_choice] #獲得選擇的商品 if p_item[1]<=salary: #判斷商品價格是否小於餘額(買的起) salary-=p_item[1] #扣錢 shooping_cart.append(p_item)#加購物車 print("您購買了 %s ,餘額剩餘 %s "%(p_item,salary)) else: print("您的餘額不足") else: print("請輸入正確的序號") elif user_choice=='q': print("---------您購買的商品列表-----") for i in shooping_cart: print(i) #列印購物列表 print("您的餘額是 %s "%salary) print("購買結束") break else: print("請輸入正確的序號")View Code
運行結果:

請輸入您的工資:10000 0 ('Iphone', 5000) 1 ('Watch', 2000) 2 ('Ipad', 3000) 3 ('Computer', 6000) 4 ('book', 50) 請選擇您要購買的商品序號(或輸入q退出):q ---------您購買的商品列表----- 您的餘額是 10000 購買結束 Process finished with exit code 0View Code
7.whlie迴圈
代碼:

1 current_number=1 2 while current_number<=5: 3 print(current_number) 4 current_number+=1View Code
讓用戶何時選擇退出

1 prompt="\n Tell me something,and i will repeat it back to you:" 2 prompt+="\nEnter 'quit' to end the program." 3 message="" 4 while message!='quit':#當輸入的不是quit時,執行迴圈。直到輸入的是quit,結束迴圈 5 message=input(prompt) 6 print(message)View Code 使用標誌:我們將變數active設置成了True(見❶),讓程式最初處於活動狀態。這樣做簡化了while語句,因為不需要在其中做任何比較——相關的邏輯由程式的其他部分處理。只要變數active為True,迴圈就將繼續運行(見❷)

1 promt="\nTell me something,and I will repeat it back to you:" 2 promt+="\nEnter 'quit' to end the program." 3 ❶active=True 4 ❷while active: #迴圈標誌,直到它的狀態為fales時停止 5 message=input(promt) 6 if message=='quit': 7 active=False 8 else: 9 print(message)View Code 使用break退出迴圈

1 promt="\nPlease enter the name of a city you hava visited:" 2 promt+="\n(Enter 'quit' when you are finished.)" 3 while True: #while True打頭的迴圈將不斷運行,直到遇到break 4 city=input(promt) 5 if city=='quit': 6 break 7 else: 8 print("I'd love to go to "+city.title()+"!")View Code 在迴圈中使用continue

1 current_number=0 2 while current_number<10: 3 current_number+=1 4 if current_number % 2 ==0: 5 continue #判斷當前數字是否為偶數,是就繼續迴圈 6 print(current_number)View Code