Python练习(2)
自己动手做一个购物商城
1.题目:
''' 程序:购物车程序 需求: 1.启动程序后,打印商品列表,显示用户信息及可用功能 2.允许用户根据商品编号购买商品 3.商品可添加至购物车,且用户可进入购物车浏览,购物车提供清空和结账功能 4.可充值,充值完毕可立即显示,并且可以随时退出 '''
2.框架:如下图所示:

总共6个函数,分别为chongzhi函数,用来提供充值功能,kehu_handle函数,用来处理各种功能,main_show函数,购物商城欢迎界面,
shopping_main函数,用来初始化购物商城,shopping_show函数,用来展示商品,ShoppingCart_show函数,用来展示用户购物车信息。
3.代码展示:
1 import json 2 import os 3 import time 4 5 def kehu_handle(handle_how,handle_who,handle_what = None): 6 ''' 7 用来处理用户的账户信息 8 :param handle_how:怎么处理 9 :param handle_who:处理哪个用户 10 :param handle_what:处理什么 11 :return:返回处理结果 12 ''' 13 if handle_how == 'clear_ShoppingCart': #清空购物车 14 with open('ShoppingCart.txt', 'r') as s: 15 ShoppingCart = json.loads(s.read()) #读取用户购物车内容 16 ShoppingCart[handle_who].clear() #清空该用户购物车内容 17 ShoppingCart = json.dumps(ShoppingCart) 18 with open('ShoppingCart.txt', 'w') as s1: 19 s1.write(ShoppingCart) #重新修改购物车清单 20 return 'Done' 21 if handle_how == 'Checkout': #结账 22 with open('Customer.txt', 'r') as c: 23 Customer = json.loads(c.read()) # 用户信息导入 24 with open('ShoppingCart.txt', 'r') as s: 25 ShoppingCart = json.loads(s.read()) 26 with open('Goods.txt', 'r') as g: 27 Goods = json.loads(g.read()) 28 kehu_balance = int(Customer[handle_who]['balance']) #获取用户余额 29 kehu_shoppingcart = ShoppingCart[handle_who] #获取用户购物车信息 30 if len(kehu_shoppingcart.keys()) != 0: #购物车不为空 31 sumprice = 0 #总价格 32 for v in kehu_shoppingcart.values(): 33 sumprice += int(v[2]) 34 if kehu_balance >= sumprice: #用户余额大于总价格 35 for k1 in list(kehu_shoppingcart.keys()): 36 v1 = kehu_shoppingcart[k1] #获取用户购买的商品信息 37 Goods[k1]["num"] -= v1[1] #商品列表的剩余数量修改 38 ShoppingCart[handle_who].clear() #清空购物车 39 kehu_balance -= sumprice #计算剩余金钱 40 Customer[handle_who]['balance'] = kehu_balance 41 Goods = json.dumps(Goods) 42 Customer = json.dumps(Customer) 43 ShoppingCart = json.dumps(ShoppingCart) 44 with open('Goods.txt', 'w') as g1: #修改商品信息 45 g1.write(Goods) 46 with open('Customer.txt', 'w') as Cu: 47 Cu.write(Customer) #将处理好的用户信息文件重新写进去 48 with open('ShoppingCart.txt', 'w') as Sh: 49 Sh.write(ShoppingCart) #将处理好的用户购物车信息重新写进去 50 return 'Done' #返回结账完成 51 else: 52 return 'Not_enough' #返回账户余额不足 53 else: 54 return 'Empty' #返回购物车为空 55 if handle_how == 'Shopping': 56 with open('ShoppingCart.txt', 'r') as s: 57 ShoppingCart = json.loads(s.read()) 58 kehu_shoppingcart = ShoppingCart[handle_who] #获取用户购物车信息 59 KhGoods_num = handle_what[1] #获取商品数量 60 KhGoods_list = handle_what[0] #获取商品列表 61 KhGoods_name = KhGoods_list[0] #获取商品名称 62 if KhGoods_name in list(kehu_shoppingcart.keys()): #购物车存在该商品,则追加 63 kehu_shoppingcart[KhGoods_name][1] += KhGoods_num 64 kehu_shoppingcart[KhGoods_name][2] = kehu_shoppingcart[KhGoods_name][0] * kehu_shoppingcart[KhGoods_name][1] 65 else: 66 kehu_shoppingcart[KhGoods_name] = [] #购物车不存在该商品则创建该商品的信息 67 kehu_shoppingcart[KhGoods_name].append(KhGoods_list[1]) 68 kehu_shoppingcart[KhGoods_name].append(KhGoods_num) 69 kehu_shoppingcart[KhGoods_name].append(kehu_shoppingcart[KhGoods_name][0] * kehu_shoppingcart[KhGoods_name][1]) 70 ShoppingCart[handle_who] = kehu_shoppingcart 71 ShoppingCart = json.dumps(ShoppingCart) 72 with open('ShoppingCart.txt', 'w') as s1: 73 s1.write(ShoppingCart) 74 return 'Done' 75 76 def main_show(user='',balance='',recharge_money=0,value=0): 77 ''' 78 购物商场欢迎界面显示 79 :param user: 用户名称 80 :param balance: 账户余额 81 :param recharge_money: 充值金额 82 :param value: 购物车个数 83 :return:无返回值 84 ''' 85 os.system('cls') 86 print(''' 87 ========================================================================== 88 * * 89 * 欢迎来到Breakering购物平台 * 90 * * 91 ========================================================================== 92 会员:{0}您好!\t您的当前余额:{1}元\t上次充值金额:{2}元\t 购物车:{3}个 93 '''.format(user,balance,recharge_money,value) #显示用户各项信息 94 ) 95 return 96 97 def shopping_show(): 98 ''' 99 商品显示详情页 100 :return: 返回商品字典 101 ''' 102 with open('Goods.txt','r') as g: 103 Goods = json.loads(g.read()) 104 tmp_dict = {} #用来临时存放商品信息 105 i = 1 106 print('商品列表'.center(70,'=')) 107 print('%-5s %-10s %-10s %-10s %-10s' % ('编号', '商品名称', '商品单价(元)', '商品总数量(个)', '商品剩余数量(个)')) 108 for k in Goods: 109 v = Goods[k] 110 if type(v) is dict: 111 print('%-5d %-20s %-15d %-20d %-10d' % (i, k, v['price'], v['sum'], v['num'])) 112 tmp_dict[i] = [k, v['price'], v['num'], v['sum']] 113 i += 1 114 print(''.center(74,'=')) 115 print('功能清单:', '\033[31;1mS\033[1m商品列表 ', '\033[31;1mG\033[1m购物选项 ', '\033[31;1mC\033[1m进入购物车 ', 116 '\033[31;1mR\033[1m充值选项 ', '\033[31;1mQ\033[1m退出 ') 117 return tmp_dict #返回商品信息,用来记录用户的选择 118 119 def ShoppingCart_show(who): 120 ''' 121 用户购物车显示页面 122 :param who: 用户名称 123 :return: 无返回值 124 ''' 125 with open('ShoppingCart.txt','r') as s: 126 ShoppingCart = json.loads(s.read()) 127 kehu_ShoppingCart = ShoppingCart[who] 128 i = 1 129 sum_price = 0 130 print('购物车列表'.center(70, '=')) 131 print('%-5s %-10s %-10s %-10s %-10s' % ('编号', '商品名称', '商品单价(元)', '购买数量(个)', '总计价格(元)')) 132 for k in kehu_ShoppingCart: 133 v = kehu_ShoppingCart[k] 134 sum_price += v[2] 135 if type(v) is list: 136 print('%-5d %-20s %-15d %-15d %-10d' % (i, k, v[0], v[1], v[2])) 137 i += 1 138 print(''.center(74, '-')) 139 print('总计%58s\033[31;1m%d\033[1m元'%('',sum_price)) 140 print(''.center(74, '=')) 141 print('功能清单:', '\033[31;1mS\033[1m返回商品列表 ', '\033[31;1mB\033[1m结账 ', '\033[31;1mC\033[1m清空购物车 ', 142 '\033[31;1mR\033[1m充值选项 ', '\033[31;1mQ\033[1m退出 ') 143 return 144 145 def chongzhi(who): 146 ''' 147 充值页面 148 :param who: 用户名称 149 :return: 无返回值 150 ''' 151 os.system('cls') 152 with open('Customer.txt','r') as c: 153 Customer = json.loads(c.read()) #用户信息导入 154 with open('ShoppingCart.txt','r') as s: 155 ShoppingCart = json.loads(s.read()) #购物车信息导入 156 user = who # 用户名称 157 balance = Customer[who]['balance'] # 账户余额 158 value = len(ShoppingCart[who].keys()) # 购物车商品个数 159 print(''' 160 ************************************************************************** 161 * 充值页面 * 162 ************************************************************************** 163 会员:{0}您好!\t 您的当前余额:{1}元\t 购物车:{2}个 164 '''.format(user,balance,value) # 显示用户各项信息 165 ) 166 while True: 167 with open('Customer.txt', 'r') as c: 168 Customer = json.loads(c.read()) 169 m = input('亲爱的用户,请输入您想充值的金额:').strip() 170 if int(m): 171 m = int(m) 172 Customer[who]['balance'] += m 173 Customer[who]['recharge_money'] = m 174 Customer = json.dumps(Customer) 175 with open('Customer.txt', 'w') as c: 176 c.write(Customer) 177 else: 178 print("亲爱的用户,您的输入有误,请重新输入!") 179 time.sleep(1) 180 continue 181 while True: 182 chongzhi_ornot = input('亲爱的用户,是否继续充值呢?[Y/N]:').strip() 183 if chongzhi_ornot == 'Y': 184 tmp = 'y' 185 break 186 if chongzhi_ornot == 'N': 187 tmp = 'n' 188 break 189 else: 190 print("亲爱的用户,您的输入有误,请重新输入!") 191 time.sleep(1) 192 continue 193 if tmp == 'y': 194 continue 195 else: 196 break 197 return 198 199 def shopping_main(who): 200 ''' 201 用来初始化购物商城 202 :param who: 用户名称 203 :return: 无返回值 204 ''' 205 with open('Customer.txt','r') as c: 206 Customer = json.loads(c.read()) #用户信息导入 207 with open('ShoppingCart.txt','r') as s: 208 ShoppingCart = json.loads(s.read()) #购物车信息导入 209 user = who #用户名称 210 balance = Customer[who]['balance'] #账户余额 211 recharge_money = Customer[who]['recharge_money'] #充值金额 212 value = len(ShoppingCart[who].keys()) #购物车商品个数 213 main_show(user,balance,recharge_money,value) 214 return 215 216 if __name__ == "__main__": #主程序启动 217 user_name = 'Breakering' 218 while True: 219 shopping_main(user_name) 220 tmp_dict = shopping_show() 221 rec = input("请输入功能编号:").strip() 222 if rec == 'S': #重新载入商品列表界面 223 continue 224 elif rec == 'Q': #退出主程序 225 break 226 elif rec == 'G': #购物选项 227 while True: 228 Goods_index = input('请输入你想添加的商品编号:').strip() 229 Goods_index = int(Goods_index) 230 Goods_num = input('请输入你想添加的数量:').strip() 231 Goods_num = int(Goods_num) 232 Goods_Surplus = int(tmp_dict[Goods_index][3]) #商品剩余数量 233 if Goods_num <= Goods_Surplus: 234 handle_what = [tmp_dict[Goods_index],Goods_num] 235 kehu_handle('Shopping',user_name,handle_what) 236 print('亲爱的用户,商品已加入购物车了哦!') 237 y_or_n = 0 238 while True: 239 continue_or_not = input('是否继续添加?[Y/N]:').strip() 240 if continue_or_not == 'Y': 241 y_or_n = 'y' #用来确定是否继续添加至购物车 242 break 243 if continue_or_not == 'N': 244 y_or_n = 'n' 245 break 246 else: 247 print('亲爱的用户,只能选择Y或者N哦!') 248 time.sleep(1) 249 continue 250 if y_or_n == 'y': 251 continue 252 else: 253 break 254 else: 255 print('亲爱的用户,商品数量不足,请重新购买哦!') 256 time.sleep(1) 257 continue 258 elif rec == 'C': #进入购物车选项 259 quit_or_not = 0 260 while True: 261 shopping_main(user_name) 262 ShoppingCart_show(user_name) 263 rec = input("请输入功能编号:").strip() 264 if rec == 'S': #返回商品列表界面 265 quit_or_not = 'n' 266 break 267 elif rec == "B": #结账功能 268 Checkout_rec = kehu_handle('Checkout',user_name) 269 if Checkout_rec == 'Done': 270 print('亲爱的用户,您已购买成功,请耐心等待发货哦!') 271 time.sleep(3) 272 continue 273 if Checkout_rec == 'Not_enough': 274 print('亲爱的用户,您的腰包不够哦!请充值!') 275 time.sleep(3) 276 continue 277 if Checkout_rec == 'Empty': 278 print('亲爱的用户,您的购物车是空的,请买点东西吧!') 279 time.sleep(3) 280 continue 281 elif rec == 'C': #清空购物车功能 282 kehu_handle('clear_ShoppingCart',user_name) 283 print('购物车清空完毕') 284 time.sleep(1) 285 continue 286 elif rec == 'R': 287 chongzhi(user_name) #用户账户充值 288 continue 289 elif rec == 'Q': 290 quit_or_not = 'y' 291 break 292 else: 293 print("亲爱的用户,您的输入有误,请重新输入!") 294 time.sleep(1) 295 continue 296 if quit_or_not == 'y': 297 break 298 else: 299 continue 300 elif rec == 'R': #充值选项 301 chongzhi(user_name) 302 continue 303 else: 304 print("亲爱的用户,您的输入有误,请重新输入!") 305 time.sleep(1) 306 continue
4.示例:
1.添加商品至购物车:

2.购物车一览:

3.购物车清空:

4.结账功能:

5.用户信息及商品信息都会随之改变

6.充值展示:

7.充值之后信息随之改变:


浙公网安备 33010602011771号