Python作業day2購物車


流程圖:

實現情況:

可自主注冊,

登陸系統可購物,充值(暫未實現),查詢余額。

擼了兩天一夜的代碼,不多說,直接上碼,注釋神馬的后面再說

  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 
購物車


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM