购物车:
用户入口:
1、商品信息存在文件里
2、判断用户是否是第一次进入,第一次进入才要输入工资;
3、已购商品,余额记录
商家入口:
1、可以添加商品,修改商品价格
1、顾客运行:user-shopping.py
2、超市老板运行:business-shopping.py3、两个文件:users记录用户金额、用户名和购买记录goods记录商品名称,金额4、购买商品请直接输入商品的英文单词两个文件:
users:
lzd,1177082,Iphone,Iphone,Iphone,Bike,Bike,Watch,Iphone,Bike,Coffee fff,11673,Iphone,Bike lkj,9960,Coffee,Coffee iii,7410,abc,abc,abc,abc,abc goods:
Iphone,5800 Mac Pro,9800 Bike,800 Watch,10600 Coffee,31 Alex Python,120 abc,987 uyi,1223 business-shopping.py
# !/usr/bin/env python# -*- coding:utf-8 -*-# Author:lzdtmpgoods=dict()# 操作打开文件,权限为只读f = open('goods','r',encoding='utf-8')# 遍历文件,读取行for line in f.readlines(): # 把当前读到的行,给字典赋值,split分割字符串生成一个列表。 tmp = line.strip().split(",") tmpgoods[tmp[0]]=tmp[1]#print(tmpgoods)f.close()exit_flag = Truewhile exit_flag: for i in tmpgoods: print(i,tmpgoods[i]) choice = input("修改商品价格直接输入商品名称>>(退出修改请按quit,增加商品请按add):") if choice in tmpgoods: choice2 = input("请输入商品价格>>:") if choice2.isdigit(): tmpgoods[choice]=choice2 else: print("商品价格请输入数字") elif choice=='quit': exit_flag=False elif choice=='add': choice3 = input("请输入商品名称>>:") choice4 = input("请输入商品价格>>:") if choice4.isdigit(): tmpgoods[choice3]=choice4 else: print("商品价格请输入数字") else: print("没有这个商品名称哦,请核实!")f = open('goods','w',encoding='utf-8')for gg in tmpgoods: f.writelines(gg+","+tmpgoods[gg]+"\n")f.close()
user-shopping.py
# !/usr/bin/env python# -*- coding:utf-8 -*-# Author:lzdtmpgoods=dict()userlist=dict()shopping_list = []k=0# 操作打开文件,权限为只读f = open('goods','r',encoding='utf-8')# 遍历文件,读取行for line in f.readlines(): # 把当前读到的行,给字典赋值,split分割字符串生成一个列表。 tmp = line.strip().split(",") tmpgoods[tmp[0]]=tmp[1]#print(tmpgoods)f.close()f = open('users','r',encoding='utf-8')# 遍历文件,读取行for line in f.readlines(): # 把当前读到的行,给字典赋值,split分割字符串生成一个列表。 tmp = line.strip().split(",") userlist[tmp[0]]=[tmp[1],tmp[2:]]#print(userlist)f.close()user = input("输入你的用户名:")while True: if user not in userlist: salary = input("输入你的工资:") if salary.isdigit(): userlist[user]=[salary,[]] break else: print("工资请输入数字!") else: salary=userlist[user][0] print("你当前余额为 \033[31;1m%s\033[0m" % salary) print("你已经购买了如下商品:") for usergoods in userlist[user][1]: print(usergoods) print("-----------------------") break#print(userlist)salary = int(salary)print("当前所有的商品列表:")while True: for index in tmpgoods: print(index,tmpgoods[index]) user_choice = input("直接输入英文,选择买啥?退出直接输入quit>>>:") if user_choice in tmpgoods: jiage = int(tmpgoods[user_choice]) if jiage <= salary: # 买的起 userlist[user][1].append(user_choice) salary -= jiage print("添加 %s 到你的购物车,你当前余额为 \033[31;1m%s\033[0m" % (user_choice, salary)) else: print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary) elif user_choice=="quit": break else: print("你输入商品名称不在列表里哦~!")userlist[user][0]=str(salary)#print(userlist)f = open('users','w',encoding='utf-8')for gg in userlist: tmpstr=gg + "," + userlist[gg][0] for kk in userlist[gg][1]: tmpstr=tmpstr+","+kk f.writelines(tmpstr+"\n")f.close()