购物车 用户入口: 1.商品信息存在文件里 2.已购商品,余额记录。存在一个文件中 商家入口: 1.可以添加商品,修改商品价格 修改后的信息存入文件,删除原文件 ''' import os def login_user(): '''用户登录''' flag = True while flag: with open('login','r',encoding='utf-8') as readings: f=str(readings.read()) for line in f: readings_str=str(f) dicts=eval(readings_str) name = input('用户名:').strip() if name in dicts : pwd = input('密码:').strip() if pwd in dicts[name]: print('欢迎%s登录'.center(50,'-')%name) salary = int(dicts[name][pwd]) print('你的余额为:%s'%salary) print('已购商品为:') with open('history','r+',encoding='utf-8')as readings: h_list=str(readings.read()) dic_history=dict(eval(h_list)) if name not in dic_history: dic_history[name]=[] else: history_list=dic_history[name] for i in dic_history[name]: print(i) lists=[] now_list=[] print('商品清单'.center(50,'-')) with open('goods','r',encoding='utf-8') as reads: for line1 in reads: lists.append(line1.split()) for index,item in enumerate(lists): print(index,str(item)) while True: choice = input('请选择你要购买的商品:') if choice.isdigit(): choice = int(choice) if choice<len(lists) and choice>=0: p_item = lists[choice] p_item[1]=int(p_item[1]) if p_item[1]<=salary: now_list.append(p_item) with open('history','r+',encoding='utf-8')as readings: history_list=dic_history[name] history_list.extend(now_list) dic_history[name]=history_list salary -= p_item[1] dicts[name][pwd]=salary with open('login','w',encoding='utf-8')as writings: writings.write(str(dicts)) print('添加%s到购物车,你的余额为\033[31;1m%s\033[0m'%(p_item,salary)) with open('history','w',encoding='utf-8') as writings: writings.write(str(dic_history)) else: print('\033[31;1m你的余额为%s,不够支付本次购买\033[0m'%salary) else: print('\033[31;1m商品序号%s输入错误,请重新输入\033[0m'%choice) elif choice == 'q': print('本次购物清单'.center(50,'-')) print(now_list) print('你的余额为%s'%salary) exit() else: print('\033[41;1m密码输入错误,请重新输入!\033[0m') continue else: print('\033[41;1m用户名输入错误,请重新输入!\033[0m') break def login_seller(): '''商户登录''' while True: name = input('商户用户名:').strip() with open('login_seller','r',encoding='utf-8') as reads: for line in reads: if line.split()[0] == name: pwd = input('请输入密码:') if line.split()[1] == pwd: print('欢迎%s登录'.center(50,'-')%name) print('商品清单'.center(50,'-')) with open('goods','r',encoding='utf-8') as readings: for line in readings: print(line.strip()) staff = input('请输入要修改的商品价格,用空格分隔(例如:iphone 5000):').strip() staff_list = staff.split() flag = False with open('goods','r') as readings , open('goods_1','w') as writings: for line in readings: if line.split()[0] == staff_list[0]: writings.write(staff+'\n') flag = True else: writings.write(line) os.remove('goods_bak') os.rename('goods','goods_bak') os.rename('goods_1','goods') if flag: print('\033[1;31m修改成功!!!\033[0m') else: print('\033[1;31m未找到商品%s信息\033[0m'%staff_list[0]) continue else: print('\033[1;31m密码错误!!!\033[0m') else: print('\033[1;31m用户名错误!!!\033[0m') def add(): '''注册用户''' newname = input('请输入新的用户名:').strip() flag = False with open('login','r') as readings: f=str(readings.read()) for line in f: readings_str=str(f) dicts=eval(readings_str) if newname in dicts: flag = True else: continue if not flag: password_salary={} password = input('请输入密码:').strip() salary = input('请输入你的工资:').strip() salary=int(salary) password_salary[password]=salary dicts[newname]=password_salary with open('login','w',encoding='utf-8') as writings: writings.write(str(dicts)) print('添加成功!!!') else: print('\033[;31m用户名已存在!!!请重新输入!!!\033[0m') def main(): action_dict = { '1':login_user, '2':login_seller, '3':add, '4':exit } print(''' 购物车系统 1.用户登录 2.商家登录 3.注册用户 4.退出 ''') while True: choice = input('>>>').strip() if choice in action_dict: action_dict[choice]() else: print('\033[31;1m请正确输入!\033[0m') continue if __name__ == '__main__': main()
程序运行效果:
存在的不足:
打开多个文件,其实可以直接用open函数。如果使用with后,把文件数据读取进程序后就可以关闭with了。比如f=str(readings.read()),这个其实后面的f其实在程序中正确作用域内都可以正常使用的。没必要一直在with里~