购物车优化要求:
用户入口:
1.商品信息存在文件里
2.已购商品,余额记录。第一次启动程序时需要记录工资,第二次启动程序时谈出上次余额
3.允许用户根据商品编号购买商品
4.用户选择商品后,检测是否够,够就直接扣款,不够就提醒
5.可随时退出,退出时,打印移购买商品和余额
商家入口:
1.可以添加商品,修改商品价格1. 运行程序输出第一级菜单
客户端入口程序代码:
import sys
def client_first_login(): #首次登录函数
client_salary=input("请输入充值金额:")
if client_salary.isdigit():
client_salary=int(client_salary)
f_client_shopping_record.write(login_name+"\t") #将购物记录写入文本
f_client_shopping_record.write(str(client_salary)+"\t")
f_client_shopping_record.write("no_product" + "\n")
for i3 in enumerate(product_lists): #循环打印列表元素及其索引
print(i3)
while True:
choice=input("请输入英雄皮肤编号,按q退出:")
if choice.isdigit():
choice=int(choice)
if choice>=0 and choice<=len(product_lists): #输入的数据在产品清单编号范围之内
product_price=int(product_lists[choice][1])
if client_salary>product_price:
shopping_list.append(product_lists[choice])
client_salary-=product_price
f_client_shopping_record.write(login_name + "\t") #在client_shopping_record中记录购物信息
f_client_shopping_record.write(str(client_salary) + "\t")
f_client_shopping_record.write(product_lists[choice][0] + "\n")
print("已购买%s,您的余额为%s"%(product_lists[choice][0],client_salary)) else:
print("对不起您的金额不足,请去充值!")
print(shopping_list)
else:
print("请输入合理的产品编号")
elif choice=="q":
print(shopping_list)
sys.exit() def client_again_login(): #再次登录函数
client_salary = input("请输入充值金额:")
if client_salary.isdigit():
client_salary = int(client_salary)+int(count_cost)
f_client_shopping_record.write(login_name + "\t")
f_client_shopping_record.write(str(client_salary) + "\t")
f_client_shopping_record.write("no_product" + "\n")
for i3 in enumerate(product_lists):
print(i3)
while True:
choice = input("请输入英雄皮肤编号,按q退出:")
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice <= len(product_lists):
product_price = int(product_lists[choice][1])
if client_salary > product_price:
shopping_list.append(product_lists[choice])
client_salary -= product_price
f_client_shopping_record.write(login_name + "\t")
f_client_shopping_record.write(str(client_salary) + "\t")
f_client_shopping_record.write(product_lists[choice][0] + "\n")
print("已购买%s,您的余额为%s" % (product_lists[choice][0], client_salary)) else:
print("对不起您的金额不足,请去充值!")
print(shopping_list)
else:
print("请输入合理的产品编号")
elif choice == "q":
print(shopping_list)
sys.exit() def product_list(): #产品清单读取
f_product_list=open("product_list","r+",encoding="utf-8")
for i4 in f_product_list:
product_lists.append(i4.split()) count_client=0
count_cost=0
count_account=0
account_password=''
product_lists=[]
shopping_list=[]
product_list()
login_name=input("请输入英雄联盟账号:")
f_client_shopping_record=open("client_shopping_record","r+",encoding="utf-8")
for i1 in f_client_shopping_record:
if login_name in i1:
count_client+=1
count_cost=i1.split()[1] f_client_account_message=open("client_account_message","r+",encoding="utf-8")
for i2 in f_client_account_message:
if login_name in i2:
count_account+=1
account_password=i2.split()[1] if count_client==0:
if count_account>0:
while True:
account_psw=input("请输入密码:")
if account_password==account_psw:
print("欢迎%s,登录英雄联盟收银台"%login_name)
client_first_login()
else:
print("密码错误请重新输入密码")
else:
print("用户名不存在")
sys.exit()
else:
print("欢迎%s,登录英雄联盟收银台,您的余额还剩%s元"%(login_name,count_cost))
client_again_login()
商家入口程序代码:
f_business=open("product_list","r+",encoding="utf-8")
f_business.write("卡莎"+"\t")
f_business.write(""+"\n")
f_business.flush() #flush函数作用:将缓冲区中的数据立刻写入文件,同时清空缓冲区
f_business.close()