1. 程式人生 > >Python隨筆3 《商城購物車》

Python隨筆3 《商城購物車》

split zha div closed line 功能 顯示 items data

這裏的購物車的程序 主要用來一些列表、文件、字符串的操作

我構思我的購物車應該具備以下幾個功能

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 《商城購物車》