PYTHON自學之路_購物車程序
阿新 • • 發佈:2017-12-18
python while 個數字 pen clas else [1] list 輸入
1 # Author:Johnson 2 3 #啟動程序後,讓用戶輸入工資,然後打印商口列表 4 #允許用戶根據商品編號購買商品,放入購物車,可用工資減去商品價格。 5 #按Q鍵退出程序,輸出選擇的商品和工資余額。 6 sp_list=[ 7 (‘蘋果手機‘,5800), 8 (‘筆記本‘,6000), 9 (‘咖啡‘, 60), 10 (‘書‘, 80), 11 (‘茶‘, 10) 12 ] 13 gwc=[] 14 gz=input("請輸入工資:") 15 if gz.isdigit(): 16 gz=int(gz) 17 else:18 print("輸入工資錯誤。") 19 exit() 20 for i in sp_list: 21 print(sp_list.index(i),i) 22 while True: 23 shuanze=input("輸入你要選擇的商品編號:") 24 if shuanze.isdigit(): 25 shuanze=int(shuanze) 26 if shuanze < len(sp_list) and shuanze>=0: 27 s_item=sp_list[shuanze]28 if s_item[1]<=gz: 29 gwc.append(s_item) 30 gz-=s_item[1] 31 print("你選擇了商品%s,還剩下%s元"%(s_item,gz)) 32 else: 33 print("工資不夠。") 34 else: 35 print("輸入的商品不存在。") 36 elif shuanze==‘Q‘: 37 fori in gwc: 38 print(i) 39 print("你還剩下%s元"%(gz)) 40 exit() 41 else: 42 print("請輸入一個數字")
PYTHON自學之路_購物車程序