購物車一、具體實現功能:1.用戶的註冊、登錄、充值、餘額查詢2.顯示商品列表、價格、庫存,顯示購物車內商品和數量3.對購物車內商品能進行減少二、具體流程圖三、代碼實現1.用到文件goods.txt1,apple,1500,1002,banana,2000,1003,orange,500,1004,p...
購物車
一、具體實現功能:
1.用戶的註冊、登錄、充值、餘額查詢
2.顯示商品列表、價格、庫存,顯示購物車內商品和數量
3.對購物車內商品能進行減少
二、具體流程圖
三、代碼實現
1.用到文件goods.txt
1,apple,1500,100 2,banana,2000,100 3,orange,500,100 4,pear,5000,100
2.為了存取方便、把goods.txt信息序列化寫入goods_1.txt,再把一個空字典序列化到user.txt,裡面存儲著用戶名、密碼、餘額(不過要先註冊才有)。
之後用到的文件就是goods_1.txt和user.txt兩個了
python2.7
#把物品信息序列化成totallist列表 import pickle def get_out(): totallist=[] #商品信息總列表 f=file('goods.txt','r') list=f.readlines() for line in list: aa=(line).strip().split(',') alist=[aa[0],[aa[1],aa[2],int(aa[3])]] totallist.append(alist) with open("goods_1.txt","w") as o: pickle.dump(totallist,o) #把新的totallist序列化寫入文件 #先寫一個空字典到賬戶文存儲件user.txt dic={} with open("user.txt","w") as o: pickle.dump(dic,o) get_out()View Code
python3.4貌似還是用二進位rb,wb來序列和反序列文件比較不會報錯
#把物品信息序列化成totallist列表 def get_out(): totallist=[] #商品信息總列表 f=open('goods.txt','rb') list=f.readlines() for line in list: aa=(line).strip().split(',') alist=[aa[0],[aa[1],aa[2],int(aa[3])]] totallist.append(alist) with open("goods_1.txt","wb") as o: pickle.dump(totallist,o) #把新的totallist序列化寫入文件 #先寫一個空字典到賬戶文存儲件user.txt dic={} with open("user.txt","wb") as o: pickle.dump(dic,o) get_out()View Code
這段代碼執行過後就可以拋棄它了(只是為了存取方便)
3、正式代碼
python2.7
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import pickle 4 import time 5 #註冊 6 def register(): 7 while True: 8 with open("user.txt","r") as o: 9 dic=pickle.load(o) 10 name=raw_input("請輸入註冊帳號:") 11 if name in dic.keys(): 12 print("帳號已存在,請重新輸入!") 13 continue 14 else: 15 pwd=raw_input("請輸入密碼:") 16 dic[name]=[pwd,0,0] 17 print("註冊成功") 18 with open("user.txt","w") as o: 19 pickle.dump(dic,o) 20 break 21 #登錄 22 def login_login():#驗證用戶名密碼是否正確 23 while True: 24 username=raw_input("請輸入賬戶名:") 25 with open("user.txt","r") as o: 26 dic=pickle.load(o) #取出已經序列化的字典,變成可讀的字典 27 if username in dic.keys(): #如果賬戶名存在 28 count=dic[username][2] #讀取字典里的輸錯次數,count為輸錯的次數 29 while True: 30 if count<3: 31 pwd=raw_input("請輸入密碼:") 32 if dic[username][0] == pwd: 33 print ("登錄成功!") 34 time.sleep(1) 35 loginmod(username) 36 break 37 else: 38 count = count+1 #錯誤一次,count加1 39 print ("密碼錯誤") 40 continue 41 else: 42 print ("帳號已被鎖定") #輸錯3次後鎖定賬戶 43 dic[username][2]=3 #把該卡號的錯誤次數改為3 44 with open("user.txt","w") as o: 45 pickle.dump(dic,o) #重新寫入文件 46 exit() 47 else: 48 print ("賬號名錯誤") 49 continue 50 break 51 #購物實現 52 def shopping(usname): 53 buy_count=0 #購買物品的總價格 54 buy_dic={} #用來放已購買物品的字典 55 buy_string="" #用來顯示購物車信息的字元串 56 with open("goods_1.txt","r") as o: 57 totallist=pickle.load(o) 58 while True: 59 print("%s購物車:%s")%("商品列表".ljust(65),buy_string) 60 print("-----------------------------------") 61 print("%-16s%-16s%-16s%-16s")%("編號","商品","價格","庫存") 62 for key in totallist: 63 print("%-14s%-14s%-14s%-14s")%(key[0],key[1][0],key[1][1],key[1][2])#列出列表裡的物品 64 print("0 : 結算 exit : 退出 q : 返回 # : 購物車") 65 print("-----------------------------------") 66 name=raw_input("請選擇要購買的商品:") 67 try: 68 if name in str(range(1,len(totallist)+1)): #如果選擇的數字在range(列表長度)中 69 things=totallist[int(name)-1][1][0] #所選商品 70 cost=int(totallist[int(name)-1][1][1]) #所選商品的價格 71 if totallist[int(name)-1][1][2]<=0: #判斷是否還有庫存 72 print("庫存不足,請重新選擇!") 73 continue 74 print("%s%s")%("你選擇了".rjust(20),things.center(6)) 75 buy_count=buy_count+cost #選擇商品總共的錢 76 if things in buy_dic.keys():#如果選擇的物品已經在購物車字典里則數量+1 77 buy_dic[things]+=1 78 else: #否則,創建一個新的key,value 79 buy_dic[things]=1 80 li_1=cut_down(buy_dic) #把購買物品的字典buy_dic傳入cut_down函數,並把返回的值賦給li_1 81 buy_string=" ".join(li_1) 82 totallist[int(name)-1][1][2] -= 1 #記憶體中商品對應的存庫減1 83 elif name == "0": 84 while True: 85 outinput=raw_input("是否結賬y/n:") 86 if outinput == "y": 87 with open("user.txt","r") as o: 88 dic=pickle.load(o) #序列化,變成可讀的字典 89 money=dic[usname][1] #該帳號的剩餘金錢 90 buy_money=buy_count #購買的商品的總價格 91 if money<buy_money: 92 update_name=raw_input("帳號餘額不足,是否修改購物車商品!y/n") 93 if update_name=="y": 94 ccc=add_to(totallist,buy_dic,buy_money) 95 totallist,buy_dic,buy_count=ccc[0],ccc[1],ccc[2] #把返回的值依次賦值 96 li_2=cut_down(buy_dic)#把購買物品的字典buy_dic傳入cut_down函數,並把返回的值賦給li_1 97 buy_string=" ".join(li_2) #把列表變成字元串來顯示 98 break 99 elif update_name=="n": 100 print ("帳號餘額不足,請充值!") 101 exit() 102 else: 103 with open("goods_1.txt","w") as o:#結賬後把記憶體里新的列表寫入文件 104 pickle.dump(totallist,o) 105 #------------------------------- 106 money=money-buy_money #購買成功後剩餘的錢 107 dic[usname][1]=money #修改剩餘的錢 108 with open("user.txt","w") as o:#新的dic寫入文件 109 pickle.dump(dic,o) 110 print ("結算成功,你購買的物品有:%s")%buy_string 111 bbb=raw_input("是否要退出程式y/n:") 112 if bbb == "y": 113 exit() 114 elif bbb=="n": 115 buy_dic={} 116 buy_string="" 117 break 118 else: 119 print ("請輸入y/n!") 120 elif outinput == "n": 121 break 122 else: 123 print ("請輸入y/n!") 124 elif name == "exit": 125 exit() 126 elif name == "#": 127 ccc=add_to(totallist,buy_dic,buy_count) 128 totallist,buy_dic,buy_count=ccc[0],ccc[1],ccc[2] 129 li_2=cut_down(buy_dic)#把購買物品的字典buy_dic傳入cut_down函數,並把返回的值賦給li_1 130 buy_string=" ".join(li_2) #把列表變成字元串來顯示 131 continue 132 elif name == "q": 133 break 134 else: 135 print("輸入錯誤!".rjust(25)) 136 except Exception: 137 print ("輸入錯誤!".rjust(25)) 138 139 #把buy_dic里的元素變成列表 140 def cut_down(buy_dic): 141 li_1=[] 142 for k,v in buy_dic.items(): 143 str="%s:%s"%(k,v) 144 li_1.append(str) 145 return li_1 146 147 #把購物車的商品放回商店 148 def add_to(totallist,buy_dic,buy_money): 149 print("購物車") 150 while True: 151 n=1 152 li_1=[] 153 for k,v in buy_dic.items(): 154 if v==0: #判斷購物車內商品是否為0,如果是則刪除刪除商品 155 buy_dic.pop(k) 156 continue 157 li=[n,[k,v]] 158 li_1.append(li) 159 n = n+1 160 print("%-12s%-12s%-12s")%("編號","商品","數量") 161 for key in li_1: 162 print("%-10s%-10s%-10s")%(key[0],key[1][0],key[1][1])#列出列表裡的物品 163 print("q 返回") 164 aa=raw_input("請選擇要退貨的商品!") 165 if aa in str(range(1,len(li_1)+1)): 166 things=li_1[int(aa)-1][1][0] #所選商品 167 if buy_dic[things]>0: #如果購物車內的商品數量大於0 168 buy_dic[things]-=1 #數量-1 169 for thin in totallist: #迴圈totallist 170 if thin[1][0]==things: #如果找到迴圈totallist內和該物品一樣時 171 id=thin[0] #找出該物品在totallist內的id 172 totallist[int(id)-1][1][2] += 1 #totallist內該物品數量回+1 173 price=totallist[int(id)-1][1][1] 174 buy_money=buy_money-int(price) 175 continue 176 else: 177 print("購物車已沒有該物品!") 178 elif aa=="q": 179 break 180 else: 181 print("輸入錯誤!") 182 return totallist,buy_dic,buy_money 183 #充值 184 def pay(usname): 185 while True: 186 input_cash=raw_input("請輸入充值金額:") 187 if input_cash.isdigit(): 188 with open("user.txt","r") as o: 189 dic=pickle.load(o) 190 money=dic[usname][1] 191 left_money=int(money)+int(input_cash) 192 print ("充值%s,餘額%s"%(input_cash,left_money)) 193 dic[usname][1]=left_money #把剩餘的錢寫入字典 194 with open("user.txt","w") as o: 195 pickle.dump(dic,o) #重新寫入文件 196 break 197 else: 198 print ("請輸入數字!") 199 200 #查詢餘額 201 def demand(usname): 202 with open("user.txt","r") as o: 203 dic=pickle.load(o) 204 money=dic[usname][1] 205 print ("你的可用餘額還有:%s")%money 206 207 #登錄成功後顯示 208 def loginmod(usname): 209 while True: 210 print '1、查詢 2、購物 3、充值 q、返回 exit、退出' 211 inp = raw_input("請選擇:") 212 if inp == "1": 213 demand(usname) 214 elif inp == "2": 215 shopping(usname) 216 elif inp == "3": 217 pay(usname) 218 elif inp == "q": 219 return 220 elif inp == "exit": 221 exit() 222 223 #主程式 224 if __name__ == "__main__": 225 while True: 226 print("1、註冊 2、登錄 exit、退出") 227 aa=raw_input("請選擇:") 228 if aa == "1": 229 register() 230 elif aa == "2": 231 login_login() 232 elif aa == "exit": 233 exit() 234 else: 235 print("輸入錯誤!")python2.7購物車
python3.4
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import pickle 4 import time 5 #註冊 6 def register(): 7 while True: 8 with open("user.txt","rb") as o: 9 dic=pickle.load(o) 10 name=input("請輸入註冊帳號:") 11 if name in dic.keys(): 12 print("帳號已存在,請重新輸入!") 13 continue 14 else: 15 pwd=input("請輸入密碼:") 16 dic[name]=[pwd,0,0] 17 print("註冊成功") 18 with open("user.txt","wb") as o: 19 pickle.dump(dic,o) 20 break 21 #登錄 22 def login_login():#驗證用戶名密碼是否正確 23 while True: 24 username=input("請輸入賬戶名:") 25 with open("user.txt","rb") as o: 26 dic=pickle.load(o) #取出已經序列化的字典,變成可讀的字典 27 if username in dic.keys(): #如果賬戶名存在 28 count=dic[username][2] #讀取字典里的輸錯次數,count為輸錯的次數 29 while True: 30 if count<3: 31 pwd=input("請輸入密碼:") 32 if dic[username][0] == pwd: 33 print ("登錄成功!") 34 time.sleep(1) 35 loginmod(username) 36 break 37 else: 38 count = count+1 #錯誤一次,count加1 39 print ("密碼錯誤") 40 continue 41 else: 42 print ("帳號已被鎖定") #輸錯3次後鎖定賬戶 43 dic[username][2]=3 #把該卡號的錯誤次數改為3 44 with open("user.txt","w") as o: 45 pickle.dump(dic,o) #重新寫入文件 46 exit() 47 else: 48 print ("賬號名錯誤") 49 continue 50 break 51 #購物實現 52 def shopping(usname): 53 buy_count=0 #購買物品的總價格 54 buy_dic={} #用來放已購買物品的字典 55 buy_string="" #用來顯示購物車信息的字元串 56 with open("goods_1.txt","rb") as o: 57 totallist=pickle.load(o) 58 while True: 59 number=[] 60 print("%s購物車:%s"%("商品列表".ljust(65),buy_string)) 61 print("-----------------------------------"