标签:
购物车
一、具体实现功能:
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()
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()
这段代码执行过后就可以抛弃它了(只是为了存取方便)
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("输入错误!")
python3.4
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import pickle 4 import time 5 import getpass 6 #注册 7 def register(): 8 while True: 9 with open("user.txt","rb") as o: 10 dic=pickle.load(o) 11 name=input("请输入注册帐号:") 12 if name in dic.keys(): 13 print("帐号已存在,请重新输入!") 14 continue 15 else: 16 pwd=input("请输入密码:") 17 dic[name]=[pwd,0,0] 18 print("注册成功") 19 with open("user.txt","wb") as o: 20 pickle.dump(dic,o) 21 break 22 #登录 23 def login_login():#验证用户名密码是否正确 24 while True: 25 username=input("请输入账户名:") 26 with open("user.txt","rb") as o: 27 dic=pickle.load(o) #取出已经序列化的字典,变成可读的字典 28 if username in dic.keys(): #如果账户名存在 29 count=dic[username][2] #读取字典里的输错次数,count为输错的次数 30 while True: 31 if count<3: 32 pwd=input("请输入密码:") 33 if dic[username][0] == pwd: 34 print ("登录成功!") 35 time.sleep(1) 36 loginmod(username) 37 break 38 else: 39 count = count+1 #错误一次,count加1 40 print ("密码错误") 41 continue 42 else: 43 print ("帐号已被锁定") #输错3次后锁定账户 44 dic[username][2]=3 #把该卡号的错误次数改为3 45 with open("user.txt","w") as o: 46 pickle.dump(dic,o) #重新写入文件 47 exit() 48 else: 49 print ("账号名错误") 50 continue 51 break 52 #购物实现 53 def shopping(usname): 54 buy_count=0 #购买物品的总价格 55 buy_dic={} #用来放已购买物品的字典 56 buy_string="" #用来显示购物车信息的字符串 57 with open("goods_1.txt","rb") as o: 58 totallist=pickle.load(o) 59 while True: 60 number=[] 61 print("%s购物车:%s"%("商品列表".ljust(65),buy_string)) 62 print("-----------------------------------") 63 print("%-16s%-16s%-16s%-16s"%("编号","商品","价格","库存")) 64 for key in totallist: 65 print("%-14s%-14s%-14s%-14s"%(key[0],key[1][0],key[1][1],key[1][2]))#列出列表里的物品 66 number.append(key[0]) 67 print("0 : 结算 exit : 退出 q : 返回 # : 购物车") 68 print("-----------------------------------") 69 name=input("请选择要购买的商品:") 70 try: 71 if name in number: #如果选择的数字在range(列表长度)中 72 things=totallist[int(name)-1][1][0] #所选商品 73 cost=int(totallist[int(name)-1][1][1]) #所选商品的价格 74 if totallist[int(name)-1][1][2]<=0: #判断是否还有库存 75 print("库存不足,请重新选择!") 76 continue 77 print("%s%s"%("你选择了".rjust(20),things.center(6))) 78 buy_count=buy_count+cost #选择商品总共的钱 79 if things in buy_dic.keys():#如果选择的物品已经在购物车字典里则数量+1 80 buy_dic[things]+=1 81 else: #否则,创建一个新的key,value 82 buy_dic[things]=1 83 li_1=cut_down(buy_dic) #把购买物品的字典buy_dic传入cut_down函数,并把返回的值赋给li_1 84 buy_string=" ".join(li_1) 85 totallist[int(name)-1][1][2] -= 1 #内存中商品对应的存库减1 86 elif name == "0": 87 while True: 88 outinput=input("是否结账y/n:") 89 if outinput == "y": 90 with open("user.txt","rb") as o: 91 dic=pickle.load(o) #序列化,变成可读的字典 92 money=dic[usname][1] #该帐号的剩余金钱 93 buy_money=buy_count #购买的商品的总价格 94 if money<buy_money: 95 update_name=input("帐号余额不足,是否修改购物车商品!y/n") 96 if update_name=="y": 97 ccc=add_to(totallist,buy_dic,buy_money) 98 print(ccc) 99 totallist,buy_dic,buy_count=ccc[0],ccc[1],ccc[2] #把返回的值依次赋值 100 li_2=cut_down(buy_dic)#把购买物品的字典buy_dic传入cut_down函数,并把返回的值赋给li_1 101 buy_string=" ".join(li_2) #把列表变成字符串来显示 102 break 103 elif update_name=="n": 104 print ("帐号余额不足,请充值!") 105 exit() 106 else: 107 with open("goods_1.txt","wb") as o:#结账后把内存里新的列表写入文件 108 pickle.dump(totallist,o) 109 #------------------------------- 110 money=money-buy_money #购买成功后剩余的钱 111 dic[usname][1]=money #修改剩余的钱 112 with open("user.txt","wb") as o:#新的dic写入文件 113 pickle.dump(dic,o) 114 print ("结算成功,你购买的物品有:%s"%buy_string) 115 bbb=input("是否要退出程序y/n:") 116 if bbb == "y": 117 exit() 118 elif bbb=="n": 119 buy_dic={} 120 buy_string="" 121 break 122 else: 123 print ("请输入y/n!") 124 elif outinput == "n": 125 break 126 else: 127 print ("请输入y/n!") 128 elif name == "exit": 129 exit() 130 elif name == "#": 131 ccc=add_to(totallist,buy_dic,buy_count) 132 totallist,buy_dic,buy_count=ccc[0],ccc[1],ccc[2] 133 li_2=cut_down(buy_dic)#把购买物品的字典buy_dic传入cut_down函数,并把返回的值赋给li_1 134 buy_string=" ".join(li_2) #把列表变成字符串来显示 135 continue 136 elif name == "q": 137 break 138 else: 139 print("输入错误!".rjust(25)) 140 except Exception: 141 print ("输入错误!".rjust(25)) 142 143 #把buy_dic里的元素变成列表 144 def cut_down(buy_dic): 145 li_1=[] 146 for k,v in buy_dic.items(): 147 str="%s:%s"%(k,v) 148 li_1.append(str) 149 return li_1 150 151 #把购物车的商品放回商店 152 def add_to(totallist,buy_dic,buy_money): 153 print("购物车") 154 while True: 155 n=1 156 li_1=[] 157 number=[] 158 for k,v in buy_dic.items(): 159 li=[n,[k,v]] 160 li_1.append(li) 161 n = n+1 162 print("%-12s%-12s%-12s"%("编号","商品","数量")) 163 for key in li_1: 164 print("%-10s%-10s%-10s"%(key[0],key[1][0],key[1][1]))#列出列表里的物品 165 number.append(str(key[0])) 166 print("q 返回") 167 aa=input("请选择要退货的商品!") 168 if aa in number: 169 things=li_1[int(aa)-1][1][0] #所选商品 170 if int(buy_dic[things])>0: #如果购物车内的商品数量大于0 171 buy_dic[things]-=1 #数量-1 172 if buy_dic[things]==0: #如果数量为0,删除该物品 173 buy_dic.pop(things) 174 for thin in totallist: #循环totallist 175 if thin[1][0]==things: #如果找到循环totallist内和该物品一样时 176 id=thin[0] #找出该物品在totallist内的id 177 totallist[int(id)-1][1][2] += 1 #totallist内该物品数量回+1 178 price=totallist[int(id)-1][1][1] 179 buy_money=buy_money-int(price) 180 continue 181 else: 182 print("购物车已没有该物品!") 183 elif aa == "q": 184 break 185 else: 186 print("输入错误!") 187 return totallist,buy_dic,buy_money 188 #充值 189 def pay(usname): 190 while True: 191 input_cash=input("请输入充值金额:") 192 if input_cash.isdigit(): 193 with open("user.txt","rb") as o: 194 dic=pickle.load(o) 195 money=dic[usname][1] #账户余额 196 left_money=int(money)+int(input_cash) #账户余额加充值的钱 197 print ("充值%s,余额%s"%(input_cash,left_money)) 198 dic[usname][1]=left_money #把剩余的钱写入字典 199 with open("user.txt","wb") as o: 200 pickle.dump(dic,o) #写入新字典 201 break 202 else: 203 print ("请输入数字!") 204 205 #查询余额 206 def demand(usname): 207 with open("user.txt","rb") as o: 208 dic=pickle.load(o) 209 money=dic[usname][1] 210 print ("你的可用余额还有:%r"%(money)) 211 212 #登录成功后显示 213 def loginmod(usname): 214 while True: 215 print (‘1、查询 2、购物 3、充值 q、返回 exit、退出‘) 216 inp = input("请选择:") 217 if inp == "1": 218 demand(usname) 219 elif inp == "2": 220 shopping(usname) 221 elif inp == "3": 222 pay(usname) 223 elif inp == "q": 224 return 225 elif inp == "exit": 226 exit() 227 228 #主程序 229 if __name__ == "__main__": 230 while True: 231 print("1、注册 2、登录 exit、退出") 232 aa=input("请选择:") 233 if aa == "1": 234 register() 235 elif aa == "2": 236 login_login() 237 elif aa == "exit": 238 exit() 239 else: 240 print("输入错误!")
四、展示
1、运行程序,因为还没有帐号,所以先注册一个,注册好后登录
2.给自己的帐号充值
3.选择购物
4.选择商品后结算
5.可以#进入购物车修改信息
6.如果余额不足以购买商品,提示是否修改购物车
6.退货完成,q返回购物列表,更新购物车和库存信息
7.余额大于商品总价格,购买成功(大致功能就这些了)
标签:
原文地址:http://www.cnblogs.com/melonjiang/p/5129012.html