您的位置:首页 > 编程语言 > Python开发

Python作业day2购物车

2020-01-31 21:28 260 查看

流程图:

实现情况:

可自主注册,

登陆系统可购物,充值(暂未实现),查询余额。

撸了两天一夜的代码,不多说,直接上码,注释神马的后面再说

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 shopping_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 ]
11 salary = 100000
12 total = 0
13 shop_list = []
14
15 while True:
16     welcome_1 = "欢迎使用XXX购物商城"
17     we_1 = welcome_1.center(30,'*')
18     print(we_1)
19     choice_1 = "1.注册 2.登陆 q.退出"
20     ch_1 = choice_1.center(32,'*')
21     exit_1 = "谢谢使用,欢迎下次光临"
22     ex_1 = exit_1.center(30,'*')
23     error_1 = "您输入的用户已存在,请重新输入"
24     e_1 = error_1.center(30,'-')
25     error_2 = "密码不能为空,请重新输入"
26     e_2 = error_2.center(30,'-')
27     error_3 = "您输入的密码太短,请重新输入"
28     e_3 = error_3.center(30,'-')
29     error_4 = "您的输入有误, 请重新输入"
30     e_4 = error_4.center(26,'*')
31     error_5 = " 您的账号已被锁定,请联系管理员 "
32     e_5 = error_5.center(12,'*')
33     print(ch_1 )
34     sr_1 = input("Please input:")
35     if sr_1 == '1':
36         while True:
37             with open('ming.txt','r')as r_1:
38                 temp = r_1.readlines()
39                 tlist = []
40             for tline in temp:
41                 tline = tline.strip().split(':')
42                 tlist.append(tline[0])
43             useradd = input("Please create user:")
44             success_1 = "成功创建用户:%s" %(useradd)
45             if useradd in tlist:
46                 print(e_1 )
47             elif useradd == "exit":
48                 break
49             else:
50                 passwd = input("Please create a password(Letters and numbers):")
51                 length = len(passwd)
52                 if length == 0 :
53                     print(e_2)
54                 elif length > 7:
55                     with open('ming.txt','a')as r_3:
56                         w_1 = '%s:%s:0\n' %(useradd,passwd)
57                         r_3.write(w_1)
58                         s_1 = success_1.center(30,'-')
59                         print(s_1)
60                         break
61                 else:
62                     print(e_3)
63
64     elif sr_1 == '2':
65         flag = False
66         while True:
67             username = input("Please enter a user name:")
68             l = open('lock.txt','r')
69             for lline in l.readlines():
70                 lline = lline.strip()
71                 if username == lline:
72                     print("账号被锁")
73                     flag = True
74                     l.close()
75                     break
76             if  flag == True:
77                 break
78
79             u = open('ming.txt','r')
80             for uline in u.readlines():
81                 user,password,mony = uline.strip().split(':')
82
83                 if username == user:
84                     i = 0
85                     while  i < 3:
86                         passwd = input('Please enter a password:')
87                         i +=1
88                         if passwd == password:
89                             print('欢迎%s登陆在线购物平台' % username)
90                             flag = True
91                             u.close()
92                             break
93                         else:
94                             if i >= 3:
95                                 with open('lock.txt','a') as l_2:
96                                     l_2.write(username + '\n')
97                                     l.close()
98                                 exit("试了太多次,将被锁定,请联系管理员")
99                             print('密码输入错误,还有%d次机会' % (3 - i))
100                     break
101             else:
102                 print("用户输入错误,请重新输入")
103
104
105             while True:
106                 print("1.购物 2.查看购物车 3.查询余额 4.充值 b.返回登陆 q.退出")
107                 print("------------------------------------------------")
108                 choice_2 = input("输入序号:")
109                 flag_1 = False
110                 while True:
111                     if choice_2 == "1":
112                         while True:
113                             for index,g in enumerate(shopping_list):
114                                 print(index,g[0],g[1])
115                             print("-------------------------")
116                             print("c.查看购物车 b.返回 q.退出")
117                             print("-------------------------")
118                             choice = input("键入数字选择商品:").strip()
119                             if choice.isdigit():
120                                 choice = int(choice)
121                                 p_price = shopping_list[choice][1]
122                                 if p_price < salary:
123                                     shop_list.append(shopping_list[choice])
124                                     total += p_price
125                                     salary -= p_price
126                                     print("-------------------------")
127                                     print("您购买了%s,余额为%s"%(shopping_list[choice][0],salary))
128                                     print("-------------------------")
129                                 else:
130                                     print("-------------------------")
131                                     print("您的余额不足")
132                                     print("-------------------------")
133                             elif choice == "c":
134                                 while True:
135                                     print("----------购物车----------")
136                                     for k,v in enumerate(shop_list):
137                                         print(k,v[0],v[1])
138                                     print("已消费金额为:%s"%total)
139                                     print("您的可用余额:%s"%salary)
140                                     print("-------------------------")
141                                     print("d.删除商品 b.返回购物 q.结算退出")
142                                     print("-------------------------")
143                                     choice_1 = input("请键入字母选择功能:")
144                                     print("-------------------------")
145                                     if choice_1 == "d":
146                                         print("-------------------------")
147                                         print("输入数字为删除商品,输入字母b为返回购物车")
148                                         print("-------------------------")
149                                         while True:
150                                             choice_2 = input("请选择:")
151                                             if choice_2.isdigit():
152                                                 choice_2 = int(choice_2)
153                                                 d_price = shop_list[choice_2][1]
154                                                 shop_list.remove(shop_list[choice_2])
155                                                 total -= d_price
156                                                 salary += d_price
157                                                 print("-------------------------")
158                                                 print("商品%s删除成功,可用余额为:%s"%(shop_list[choice_2][0],salary))
159                                                 print("-------------------------")
160                                             elif choice_2 == "b":
161                                                 break
162                                     elif choice_1 == "b":
163                                         flag = True
164                                         break
165                                     else:
166                                         print("----------购物清单----------")
167                                         for k,v in enumerate(shop_list):
168                                             print(k,v[0],v[1])
169                                         print("总消费金额为:%s"%total)
170                                         print("您的可用余额:%s"%salary)
171                                         print("--------欢迎下次再来-------")
172                                         exit(0)
173                             elif choice == "b":
174                                 break
175                             elif choice == "q":
176                                 print("----------购物清单----------")
177                                 for k,v in enumerate(shop_list):
178                                     print(k,v[0],v[1])
179                                 print("总消费金额为:%s"%total)
180                                 print("您的可用余额:%s"%salary)
181                                 print("--------欢迎下次再来--------")
182                                 exit(0)
183                             else:
184                                 print("-------------------------")
185                                 print("您的输入有误,请重新输入")
186                                 print("-------------------------")
187                         if flag == True:
188                             break
189                     elif choice_2 == "2":
190                         print("----------购物车----------")
191                         for k,v in enumerate(shop_list):
192                             print(k,v[0],v[1])
193                         print("已消费金额为:%s"%total)
194                         print("您的可用余额:%s"%salary)
195
196                         print("-------------------------")
197                         break
198                     elif choice_2 == "3":
199                         with open('ming.txt','r')as m_1:
200                             mony_1 = m_1.readlines()
201                         for mline in mony_1:
202                             (user,password,mony) = mline.strip().split(':')
203                             print(salary)
204                             flag_1 = True
205                             break
206                         break
207
208                     elif choice_2 == "4":
209                         z  = 0
210                         while z < 1:
211                             chongzhi = int(input("输入金额:"))
212                             passwd_1 = input("请输入密码:")
213                             m = open('ming.txt','r+')
214                             m_2 = m.readlines()
215
216                             for mline in m_2:
217                                 (user,password,mony) = mline.strip().split(':')
218
219                                 if passwd_1 == password:
220                                     mony_2 = (chongzhi + int(mony))
221
222                                     w_2 = '%s:%s:%s\n' %(username,password,mony_2)
223                                     m.write(w_2)
224                                     print("充值成功")
225                                     print(mony)
226                                     flag = True
227                                     break
228                                 continue
229                             break
230                         if flag == True:
231                             break
232
233                     elif choice_2 == "b":
234                         flag = True
235                         break
236
237                     elif choice_2 == "q":
238                         exit(ex_1)
239                     else:
240                         print(e_4)
241                         break
242                     break
243                 if flag == True:
244                     break
245             break
246     elif sr_1 == 'q':
247         exit(ex_1)
248     else:
249         print(e_4)
250         print('                                   ')
251
252
253
购物车

转载于:https://www.cnblogs.com/spykids/p/5163108.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
aluf9113078 发布了0 篇原创文章 · 获赞 0 · 访问量 65 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: