Python随笔3 《商城购物车》

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python随笔3 《商城购物车》相关的知识,希望对你有一定的参考价值。

这里的购物车的程序 主要用来一些列表、文件、字符串的操作 

我构思我的购物车应该具备以下几个功能

1.能够新注册用户,注册时检测用户名唯一。

2.能够进行用户登陆,多次密码输入错误锁定用户。

3.可以浏览商品,显示价格,可将商品进行添加购物车、移除购物车 ,结算等

4.检测账户余额,进行账户充值。

流程图画在草稿上不小心丢了哈哈,直接上代码好了,记录一下自己花了一天的成果。

技术分享
  1 # -*- coding:utf-8 -*-
  2 
  3 items_list = [
  4     [Iphone 6s plus, 5800],
  5     [Lumia, 3800],
  6     [Charge, 45],
  7     [Data line, 35],
  8     [MI 5 PRO, 2299],
  9     [MX4, 1999],]
 10 salary = 0
 11 total = 0
 12 shop_list = []
 13 while True:
 14     flag = 0
 15     title_1 = 欢迎来到英雄商城
 16     t_1 = ‘‘.center(58,X)
 17     t_2 = title_1.center(50,-)
 18     t_3 = "-"*70
 19     #success print
 20     success_1 = "您已成功注册账户"
 21     s_1 = success_1.center(20,-)
 22     success_2 =欢迎登陆商城
 23     s_2= success_2.rjust(20,-)
 24     #error print
 25     error_1 ="您输入的账户已存在,清重新输入"
 26     er_1= error_1.center(20,"-")
 27     error_2 ="输入密码不一致,重新注册"
 28     er_2= error_2.center(20,"-")
 29     error_3 = 您的账户已经被锁定!重新输入或者联系管理员
 30     er_3 = error_3.center(20,-)
 31     error_4 = 输入的账户密码不匹配,您总共有三次尝试机会
 32     er_4= error_4.center(20,-)
 33     error_5 = 账户不存在
 34     er_5 = error_5.center(20,-)
 35     print(t_1,\n,t_2,\n,t_1)
 36     choice = 1.注册用户;2.登陆用户;3.退出程序
 37     ch_1 = choice.center(20,-)
 38     print(ch_1)
 39     st_1 = input(输入您的选择:)
 40     if st_1 == 1:
 41         while True:
 42             if flag == 1:
 43                 break
 44             with open(zhanghumima.txt,r) as f_1:
 45                 list = []
 46                 for line in f_1.readlines():
 47                     tline = line.strip().split(:)
 48                     list.append(tline[0])
 49             user_add = input("===输入注册账户名称:")
 50             if user_add in list:
 51                 print(er_1)
 52                 break
 53             elif user_add == exit:
 54                 break
 55             else:
 56                 while flag==0:
 57                     password_1 = input("===请输入您的密码:")
 58                     password_2 = input("===请输入您的密码again:")
 59                     if password_1 ==password_2:
 60                         with open(zhanghumima.txt,a) as f_2:
 61                             w_1 = "%s:%s:0\n" %(user_add,password_1)
 62                             f_2.write(w_1)
 63                             print(s_1)
 64                             flag =1
 65                     else:
 66                         print(er_2)
 67                         break
 68     elif st_1 == 2:
 69         flag = 0
 70         while True:
 71             if flag ==1:
 72                 break
 73             username = input("请输入账户名称:")
 74             with open(lock.txt,r) as f_3:
 75                 lock_lt = []
 76                 for line in f_3.readlines():
 77                     lock_lt.append(line.strip())
 78                 if username in lock_lt:
 79                     print(er_3)
 80                     break
 81             with open(zhanghumima.txt,r) as f_3_4:
 82                 zhanghu_1=[]
 83                 for line in f_3_4:
 84                     zhanghu = []
 85                     zhanghu.append(line.strip().split(:))
 86                     zhanghu_1.append(zhanghu[0][0])
 87                 if username not in zhanghu_1:
 88                     print(er_5)
 89                     break
 90             with open(zhanghumima.txt,r) as f_4:
 91                 for line in f_4.readlines():
 92                     user,password,money=line.strip().split(:)
 93                     if username==user:
 94                         i=0
 95                         while i < 3:
 96                             password_3 = input(请输入您的密码:)
 97                             i+= 1
 98                             if password_3 ==password:
 99                                 print(s_2,username,-*20)
100                                 salary = int(money)
101                                 flag = 1
102                                 break
103                             else:
104                                 print(密码不对,您还有%d次机会 %(3-i))
105                         while i >=3:
106                             with open(lock.txt,a) as a_1:
107                                 a_1.write(username+"\n")
108                                 exit(错误次数太多,账户一辈锁定,请联系管理员)
109             while True:
110                 with open(zhanghumima.txt, r) as f_4:
111                     for line in f_4.readlines():
112                         user, password, money = line.strip().split(:)
113                         if username == user:
114                             salary = int(money)
115                 print(t_3)
116                 print("1.购物 2.查看购物车 3.查询余额 4.充值 b.返回登陆 q.退出")
117                 print(t_3)
118                 choice_2 = input(请输入您的选择:)
119                 flag_1 = 0
120                 while True:
121                     if choice_2=="1":
122                         for index,g in enumerate(items_list):
123                             print(index,g[0],g[1])
124                         print(t_3)
125                         print(c.查看购物车 b.返回 q.退出)
126                         print(t_3)
127                         choice_3 = input(选择对应的商品).strip()
128                         if choice_3.isdigit():
129                             choice = int(choice_3)
130                             price = items_list[choice][1]
131                             if price<=salary:
132                                 shop_list.append(items_list[choice])
133                                 total += price
134                                 salary -= price
135                                 print("您购买了%s,余额为%d" %(items_list[choice][0],salary))
136                             else:
137                                 print(t_3)
138                                 print(余额不足)
139                                 print(t_3)
140                         elif choice_3 == c:
141                             while True:
142                                 print(t_3)
143                                 print(这里是购物车)
144                                 print(t_3)
145                                 for k,v in enumerate(shop_list):
146                                     print (k,v[0],v[1])
147                                 print(已消费金额为%d %total)
148                                 print(剩余金额为%d %salary)
149                                 print(t_3)
150                                 print("d.删除商品 b.返回购物 q.结算退出")
151                                 print(t_3)
152                                 choice_4 = input(输入您的选项:)
153                                 if choice_4 ==d:
154                                     print(输入的数字为的删除的对应商品,输入b返回)
155                                     while True:
156                                         choice_5 = input(您选择:)
157                                         if choice_5.isdigit():
158                                             choice_5 = int(choice_5)
159                                             shop_list.remove(shop_list[choice_5])
160                                             total -=price
161                                             salary+=price
162                                             print(删除商品成功)
163                                         elif choice_5 ==b:
164                                             break
165                                 elif choice_4==b:
166                                     break
167                                 elif choice_4==q:
168                                     print(t_3)
169                                     print(购物清单)
170                                     print(t_3)
171                                     for k,v in enumerate(shop_list):
172                                         print(序号, k, \t 物品, v[0], \t 价格, v[1])
173                                         print(总消费金额为%d %total)
174                                         print(消费成功,欢迎下次光临!!!)
175                                         print(t_3)
176                                         exit(0)
177                         elif choice_3 == b:
178                             break
179                         elif choice_3 == q:
180                             exit(欢迎下次光临)
181                         else:
182                             print("您输入的格式不对,请再考虑考虑")
183                     elif choice_2 ==2:
184                         print(t_3)
185                         print(这里是购物车)
186                         print(t_3)
187                         for k, v in enumerate(shop_list):
188                             print(k, v[0], v[1])
189                         print(已消费金额为%d % total)
190                         print(剩余金额为%d % salary)
191                         break
192                     elif choice_2 == 3:
193                         while True:
194                             with open(zhanghumima.txt, r) as f_5:
195                                 for line in f_5.readlines():
196                                     i,j,k =line.strip().split(:)
197                                     if username==i:
198                                         print(亲爱的%s,您的账户有%s元钱! % (i, k))
199                             break
200                         break
201                     elif choice_2 =="4":
202                         while True:
203                             recharge = int(input(请输入充值金额:))
204                             list_1 =[]
205                             with open(zhanghumima.txt, r) as r_3:
206                                 for line in r_3.readlines():
207                                     list_1.append(line.strip().split(":"))
208                                 # print(list_1)
209                                 for item in list_1:
210                                     if item[0] == username:
211                                         new_money = int(item[2])+recharge
212                                         new_money = str(new_money)
213                                         item[2] =new_money
214                                         print(充值成功当前余额:,item[2])
215                             f=open(zhanghumima.txt,w)
216                             f.truncate()
217                             f.close()
218                             with open(zhanghumima.txt,a) as w_2:
219                                 for item in list_1:
220                                     wri =%s:%s:%s\n %(item[0],item[1],item[2])
221                                     w_2.writelines(wri)
222                                 break
223                             break
224                         break
225 
226                     elif choice_2 == b:
227                         break
228                     elif choice_2 == q:
229                         print(t_3)
230                         print(购物清单)
231                         print(t_3)
232                         for k, v in enumerate(shop_list):
233                             print(序号, k,\t 物品, v[0],\t 价格, v[1])
234                             print(总消费金额为%d % total)
235                             print(消费成功,欢迎下次光临!!!)
236                             print(t_3)
237                             exit(0)
238 
239 
240 
241 
242     elif st_1 == 3:
243         exit(“感谢使用英雄商城购物系统,欢迎再次使用”)
244     else:
245         print(输入类型不对,检查一下)
购物车程序代码

 

以上是关于Python随笔3 《商城购物车》的主要内容,如果未能解决你的问题,请参考以下文章

Python作业之购物商城

Python_购物商城ATM

Python_购物商城ATM

Python开发程序:ATM+购物商城

Python__模拟实现一个ATM+购物商城程序

Python实战之网上银行及购物商城