加入了会员打折优惠机制 并加入推荐机制(根据买的奶茶次数最多的进行推荐)
def set_calculate(setbuy):
temp1 = 0
temp2 = 0
temp3 = set()
jiao_temp = set()
supple_set = set()
for i in setlist.values():
jiao_temp = i & setbuy
supple_set = i.difference(setbuy)
if len(jiao_temp) >= temp1 and len(supple_set) > temp2:
temp1 = len(jiao_temp)
temp2 = len(supple_set)
temp3 = i
if len(supple_set)+len(setbuy) == 5:
break
supple_set = temp3.difference(setbuy)
temp4 = 0
for i in supple_set:
if int(i) > temp4:
temp4 = i
return temp4
def judge_recommend(setbuy,snum,temp4):
if temp4 != 0:
print('根据其他顾客的购买情况,向您推荐奶茶口味:'+str(temp4)+') '+str(menu[temp4][0]))
整体代码:
import time
import csv
menu={'1':('原味冰奶茶',3),'2':('香蕉冰奶茶',5),'3':('草莓冰奶茶',5),'4':('蒟蒻冰奶茶',7),'5':('珍珠冰奶茶',7)}
buylist={}
vipnum={}
setbuy=set()
setlist={}
srecord=[]
snum=0
count=1
tem1='本店近期有一款爆款网红奶茶:蒟蒻冰奶茶,购买编号为4哦~'
def csvtest(vipnum,srecord):
with open('会员信息.csv','w',encoding='utf-8',newline='') as fvip:
csv_writervip = csv.writer(fvip)
csv_writervip.writerow(["手机号","出生日期","性别","星座","所在地","注册日期"])
for i in vipnum:
csv_writervip.writerow(vipnum[i])
with open('购买信息.csv','w',encoding='utf-8',newline='') as fbuy:
csv_writerbuy = csv.writer(fbuy)
csv_writerbuy.writerow(["序号","奶茶口味","数量","总价","购买日期"])
for j in srecord:
csv_writerbuy.writerow(j)
def printmenu():
print("欢迎光临盛乐奶茶馆~")
for i in menu.keys():
print(str(i)+") "+str(menu[i][0])+str(menu[i][1])+"元")
return
def price (buylist,count):
sump=0
for i in buylist.keys():
p=menu[i][1]*int(buylist[i])*count
sump=sump+p
print("您购买了"+str(buylist[i])+"杯 "+i+') '+str(menu[i][0])+",共计"+str(p)+"元")
return sump
def invip ():
while True:
vip=input("请输入会员号码:")
if(len(vip)==11):
break
else:
print("号码有误,请输入11位手机号码哦~")
if int(vip) in vipnum :
count=0.9
print("尊敬的会员您好,您本次购买可享受会员价优惠!")
else :
vipnum[vip]=vip
count=1
print("请完善您的会员信息~")
birth=input("出生日期:")
sex=input("性别:")
star=input("星座:")
place=input("所在地:")
fdays=time.strftime('%Y-%m-%d',time.localtime(time.time()))
vipmess=[vip,birth,sex,star,place,fdays]
vipnum[vip]=vipmess
print("会员信息已录入,下次购买即可享受会员优惠哦!")
return vipnum,count
def buy ():
while True:
choose=input("请选择奶茶口味:")
if int(choose)>=1 and int(choose)<=5:
setbuy.add(choose)
many=input("请输入购买数量:")
break
else :
print("Woops! 我们只售卖以上五种奶茶哦!新口味敬请期待!")
return many,choose
def add (snum,menu,choose,many,sump,srecord):
fdays=time.strftime('%Y-%m-%d',time.localtime(time.time()))
record=[str(snum),str(menu[choose][0]),str(many),str(sump),fdays]
srecord.append(record)
return
def water (srecord):
print("序号 奶茶口味 数量 总价 购买日期")
print(srecord)
return
def set_calculate(setbuy):
temp1 = 0
temp2 = 0
temp3 = set()
jiao_temp = set()
supple_set = set()
for i in setlist.values():
jiao_temp = i & setbuy
supple_set = i.difference(setbuy)
if len(jiao_temp) >= temp1 and len(supple_set) > temp2:
temp1 = len(jiao_temp)
temp2 = len(supple_set)
temp3 = i
if len(supple_set)+len(setbuy) == 5:
break
supple_set = temp3.difference(setbuy)
temp4 = 0
for i in supple_set:
if int(i) > temp4:
temp4 = i
return temp4
def judge_recommend(setbuy,snum,temp4):
if temp4 != 0:
print('根据其他顾客的购买情况,向您推荐奶茶口味:'+str(temp4)+') '+str(menu[temp4][0]))
def recorded (buylist,ta1,ta2,ta3,ta4,ta5):
for i in buylist.keys:
if(i=='1'):
ta1[vipnum]=buylist[i]
elif(i=='2'):
ta2[vipnum]=buylist[i]
elif(i=='3'):
ta3[vipnum]=buylist[i]
elif(i=='4'):
ta4[vipnum]=buylist[i]
elif(i=='5'):
ta5[vipnum]=buylist[i]
if __name__ == '__main__':
x=time.strftime('%Y-%m-%d',time.localtime(time.time()))
sdays=1
while True:
y=x
x=time.strftime('%Y-%m-%d',time.localtime(time.time()))
cmp=0
if(x!=y):
snum=0
sdays=sdays+1
while snum<2:
if sdays==30:
print("系统需要维护,请重新启动。")
cmp=1
break
printmenu()
snum=snum+1
setbuy=set()
print(tem1)
v=input("本店会员可享受会员专属折扣哦~请问您是否要注册会员?Y/N:")
if v=='Y' or v=='y':
vipnum,count=invip()
while True:
many,choose=buy()
temp4 = set_calculate(setbuy)
judge_recommend(setbuy,snum,temp4)
if(choose in buylist):
buylist[choose]=buylist[choose]+int(many)
else:
buylist[choose]=int(many)
nextchoose=input("是否要继续购买?继续请输入Y,结束购买请输入N:")
if(nextchoose=='N' or nextchoose=='n'):
setlist[snum]=setbuy
print("您是今天的第"+str(snum)+"位顾客。")
sump=price(buylist,count)
add (snum,menu,choose,many,sump,srecord)
print("谢谢惠顾,欢迎下次光临~")
break
snum=snum+1
if cmp==0:
print("您是今天的第"+str(snum)+"位顾客。")
print("今日已闭店,欢迎您明天光临!")
pingzheng=input("如需打印今日流水请输入Y:")
if pingzheng=='y' or pingzheng=='Y':
water(srecord)
本文中的代码有些许不完善仅供学习参考
|