python 购物车+一点点优化
要求:
1.卖家
添加商品,编辑商品列表。
2.买家
从文件读历史记录,记余额。
customer:
#!/usr/local/bin/python3.5 ''' goods file: #edit by business just read goods = [ ["Apple", 5888], ["Sharbak", 30], ["MacBook", 12888], ["Bike", 3000] ] user file: my_goods = [ 100000, #第一个放初始化余额 ["Apple", 5888, 94112], ["Bike", 3000, 91112] ] ''' import pickle import os import sys import copy goods_file = "goods_file" user_file = "user.file" balance = 0 shopping_list = [] history_list = [] end_index = 0 # 1. 通过文件操作判断商品文件和用户私有文件 if os.path.exists(goods_file) == False: #或者文件为空 print("File not enough!") sys.exit(0) else:#1.2 读取商品列表 with open(goods_file, 'rb') as fd: goods_list = pickle.load(fd) fd.close() if len(goods_list) == 0: print("Empty goods list!") sys.exit(0) if os.path.exists(user_file) == False: os.system('touch %s'%user_file) balance = input("input your balance(q to quit):") if balance.isdigit(): balance = int(balance) history_list.append(balance) elif balance == 'q': sys.exit(0) else: print("输错了,你自己重启吧~") sys.exit(0) else:# 1.3 私有数据(购买记录 余额) with open(user_file, 'rb') as fd: history_list = pickle.load(fd) fd.close() balance = int(history_list[-1][-1][2]) end_index = len(history_list) # 3. 购买 while True: # 3.1 show 商品信息和余额 for index, gl in enumerate(goods_list): print("\n", index, gl) print("Your balance:%d"%balance) uc = input("Input your choice(q to quit):") if uc.isdigit() == True: uc = int(uc) if uc < len(goods_list) and uc >= 0: item = copy.deepcopy(goods_list[uc]) if int(item[1]) <= balance: balance -= int(item[1]) item.append(balance) shopping_list.append(item) else: print("Your balance is not enough for this commodity!") else: print("Invaild input!") elif uc == 'q': break else: print("Invaild input!") # 4.show 购买列表 和 余额 print("\n 这是你的购买清单") for index, gl in enumerate(shopping_list): print(index,gl) history_list.append(shopping_list) # 5. 更新到用户文件 with open(user_file, 'wb') as fd: #打开文件覆盖旧内容 pickle.dump(history_list, fd) fd.close()
business:
#! /usr/local/bin/python3.5 ''' goods file: #edit by business just read goods = [ ["Apple", 5888], ["Sharbak", 30], ["MacBook", 12888], ["Bike", 3000] ] ''' import pickle import os import copy goods_file = "goods_file" goods_list = [] IsEmpty = True # 1. 判断文件是否存在 if os.path.exists(goods_file) == False: #创建文件 os.system('touch %s'%goods_file) IsEmpty = True else: with open(goods_file, 'rb') as fd: #打开文件读内容 goods_list = pickle.load(fd) fd.close() IsEmpty = False item = ['',''] # 2. 编辑商品信息 while True: if IsEmpty == False: print("\nThis is your goods list:") for index,gl in enumerate(goods_list): print(index, gl) uc = input("a. add d.delete e.edit q.quit :") if len(uc) > 1: print("Invalied input!") elif uc.isalpha() == False: print("Invalied input!") elif uc != 'a' and uc != 'd' and uc != 'e' and uc != 'q': print("Invalied input!") else: if uc == 'a': item[0] = input("Input goods' name:") item[1] = input("Input goods' price:") goods_list.append(copy.deepcopy(item)) IsEmpty = False elif uc == 'd': choice = input(" Do you want delete witch one:")#不写参数判断了 choice = int(choice) del goods_list[choice] elif uc == 'e': choice = input(" Do you want edit witch one:") choice = int(choice) item[0] = input("Input goods' name:") item[1] = input("Input goods' price:") del goods_list[choice] goods_list.append(copy.deepcopy(item)) elif uc == 'q': break # 3. 写到文件 with open(goods_file, 'wb') as fd: #打开文件覆盖旧内容 pickle.dump(goods_list, fd) fd.close() # finish

浙公网安备 33010602011771号