首页 > 代码库 > 购物车程序
购物车程序
购物车程序
要求:
- 有存放用户信息的文件,三次登录判断用户锁定
- 有商品列表文件,历史记录问题,购物车文件
- 查看商品时要求分页显示,每页显示数量自定义
- 可以查看购物记录
思路:
-----------------------------------------------------------------
购物车程序BUG:
1,程序执行完成后,文件没有被更新;
2,在查看历史记录的时候,提示的当前页码数,显示为0,不得不+1
3,没有实现根据关键字搜索功能
代码:
#!/usr/bin/env python # -*- coding:utf-8 -*- # # 读取用户信息 d1 = open(‘user_db‘, ‘r‘, encoding=‘UTF-8‘) d2 = d1.read() d1.close() v1 = d2.split(‘\n‘) user_list = [] user_balance = [] for i in v1: v2 = i.split(‘|‘) user_list.append({ ‘name‘: v2[0], ‘pwd‘: v2[1], ‘t‘: v2[2], ‘balance‘: v2[3] }) flag = True while flag: in_user = input(‘请输入用户名(q退出程序):‘) if in_user != ‘q‘: for item in user_list: if in_user == item[‘name‘]: if int(item[‘t‘]) < 3: in_pwd = input(‘请输入密码:‘) if in_pwd == item[‘pwd‘]: item[‘t‘] = 0 user_balance.append(item[‘balance‘]) print(‘登录成功,您的账户余额为:%s‘ % (item[‘balance‘])) flag = False break else: nt = 2 - int(item[‘t‘]) print(‘密码错误,还剩%s尝试次数‘ % nt) item[‘t‘] = int(item[‘t‘]) + 1 break else: print(‘此用户已被锁定‘) exit(‘程序退出‘) else: print(‘用户不存在,请重新输入:(q退出程序)‘) else: flag = False # 读取商品文件信息 a1 = open(‘goods‘, ‘r‘, encoding=‘UTF-8‘) a2 = a1.read() a1.close() # 读取历史文件信息 b1 = open(‘history‘, ‘r‘, encoding=‘UTF-8‘) b2 = b1.read() b1.close() history_list = [] # 读取购物车文件信息 c1 = open(‘cart‘, ‘r‘, encoding=‘UTF-8‘) c2 = c1.read() c1.close() cart_list = [] # 将读取到的商品列表信息转换为一个列表 goods_list = [] goods_1 = a2.split(‘\n‘) for item_1 in goods_1: item_2 = item_1.split(‘:‘) goods_list.append({ ‘name‘: item_2[0], ‘price‘: item_2[1] }) # 判断当前商品列表页码范围 a = int(len(goods_list)) b = int(a % 10) c = a // 10 if b != 0: d = c + 1 else: d = c # 判断当前历史记录页码范围 e = int(len(history_list)) f = int(e % 10) g = e // 10 if f != 0: h = g + 1 else: h = g # 分页显示列表 flag = True while flag: choice = input(‘1:选购商品;2:删除购物车商品;3:结算购物车商品;4:查看购买记录;q:退出程序\n你的选择是:‘) while flag: if choice == ‘1‘: x = input(‘请输入页码查看商品列表(共%s页;b返回;q退出):‘ % (d,)) if x.isdecimal(): x = int(x) start = (x - 1) * 10 end = x * 10 goods_a = goods_list[start:end] goods_list_1 = [] for i in goods_a: goods_list_1.append(i) for j, k in enumerate(goods_list_1, 1): print(j, k) while flag: buy_choice = input(‘请选择要购买的商品序号:‘) buy_choice = int(buy_choice) - 1 cart_list.append(goods_list_1[buy_choice]) print(‘已添加到购物车列表\n已选%s‘ % (cart_list,)) break elif x == ‘b‘: break elif x == ‘q‘: print(‘程序退出‘) exit(‘bye‘) elif choice == ‘2‘: print(‘当前购物车商品:‘) for o, p in enumerate(cart_list, 1): print(o, p) cart_list_sum1 = 0 for k in cart_list: cart_list_sum1 += int(k[‘price‘]) print(‘当前购物车商品总金额%s:‘ % (cart_list_sum1,)) del_choice = input(‘请选择要删除的商品序号(b返回):‘) if del_choice != ‘b‘ and int(del_choice) >= 0: del_choice = int(del_choice) - 1 cart_list.remove(cart_list[del_choice]) print(cart_list) else: print(‘输入有误‘) break elif choice == ‘3‘: cart_list_sum2 = 0 for k in cart_list: cart_list_sum2 += int(k[‘price‘]) history_list.append(k) print(‘当前购物车商品总金额:%s\n您的账户余额:%s‘ % (cart_list_sum2, user_balance)) user_balance_int = int(user_balance[0]) if user_balance_int >= cart_list_sum2: user_balance_int -= cart_list_sum2 print(‘结算成功,当前余额:%s‘ % (user_balance_int,)) break else: print(‘余额不足‘) break elif choice == ‘4‘: x1 = input(‘请输入页码查看商品列表(共%s页;b返回;q退出):‘ % (h,)) if x1.isdecimal(): x1 = int(x1) start = (x1 - 1) * 10 end = x1 * 10 history_a = history_list[start:end] history_list_1 = [] for i1 in history_a: history_list_1.append(i1) for j1, k1 in enumerate(history_list_1, 1): print(j1, k1) #print(history_list) break elif choice == ‘q‘: print(‘一路顺风‘) exit(‘bye‘) else: print(‘抱歉,输入有误‘) break # 更新用户登录信息文件 user_list_new = [] for m in user_list: v3 = ‘%s|%s|%s‘ % (m[‘name‘], m[‘pwd‘], m[‘t‘]) user_list_new.append(v3) v4 = user_list_new[0] + ‘\n‘ + user_list_new[1] d3 = open(‘db‘, ‘w‘) d3.write(v4) d3.close() # 更新历史记录文件 history_list_new = [] for n in history_list: b3 = ‘%s:%s‘ % (n[‘name‘], n[‘price‘]) history_list_new.append(b3) b4 = history_list_new[0] + ‘\n‘ + history_list_new[1] b5 = open(‘history‘, ‘w‘) b5.write(b4) b5.close() # 更新购物车文件 cart_list_new = [] for o in cart_list: c3 = ‘%s:%s‘ % (o[‘name‘], o[‘price‘]) cart_list_new.append(c3) c4 = cart_list_new[0] + ‘\n‘ + cart_list_new[1] c5 = open(‘cart‘, ‘w‘) c5.write(c4) c5.close()
--------
附商品列表信息文件goods
USP.45:500 DesertEagle 沙漠之鹰:650 M3 Super90 Combat:1700 AK47:2500 M4A1:3100 AUG A1:3500 ScoutSniperRifle:2750 AWP:4750 M249:5750 Glock18:400 SIG P228:600 M4 Super90 xm1014:3000 MP5 Navy:1500 Steyr TMP:1250 Fabrique Nationale:2350 UMP 45:3500 Mac 10 乌兹:1400 SG-552:3500 SIG 550:4200 Scout:3200
购物车程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。