'''
购物车(注册、登录、购物、购物车、结帐)
'''
shopping_car_dict=dict()
money=[0]
def input_username_pwd():
username=input('username:')
pwd=input('pwd:')
return username,pwd
def goods_get():
with open('goods.txt','r',encoding='utf8')as fr:
fr1=fr.read()
fr2=eval(fr1)
return fr2
fr2=goods_get()
def register():
print('欢迎来到注册功能:')
username,pwd=input_username_pwd()
with open('user_info.txt','a',encoding='utf8')as fa:
fa.write(f'{username}:{pwd}|')
def login():
print('欢迎来到登录功能:')
username,pwd=input_username_pwd()
username1=f'{username}:{pwd}'
with open('user_info.txt', 'r', encoding='utf8')as fr:
fr2=fr.read()
username2=fr2.split('|')
if username1 in username2:
print('登录成功')
else:
print('密码不对')
def shopping():
print('欢迎来到购物天堂:')
while True:
for ind,goods in enumerate(fr2):
print(f'商品编号{ind}',goods)
choice=input('请选择商品编号,退出请按q:')
if choice=='q':
break
choice=int(choice)
goods=fr2[choice]
print(f'购买商品{goods}成功,
')
goods_name=goods[0]
if goods_name in shopping_car_dict:
shopping_car_dict[goods_name]+=1
else:
shopping_car_dict[goods_name]=1
money[0]+=goods[1]
print(f'你购买的商品是{shopping_car_dict},总价是{money[0]}')
def shopping_car():
print('欢迎来到购物车功能:')
while True:
print(f'你购买的商品是{shopping_car_dict},总价是{money[0]}')
choice=input('请输入你要删除的商品编号,退出请按q:')
if choice=='q':
break
shopping_car_dict[choice]-=1
for goods in fr2:
for choice in goods:
money[0]-=goods[1]
def pay():
print('欢迎来到付款中心')
print(f'你购买的商品是{shopping_car_dict},总价是{money[0]}')
choice=input('确认付款请按Y或y,清空购物车请按N或n:')
if choice=='Y'or choice=='y':
print(f'你已支付{money[0]}元,购买的商品是{shopping_car_dict}')
elif choice=='N'or choice=='n':
shopping_car_dict.clear()
money[0]=0
print('购物车已清空')
else:
print('输入不合法')
func_msg='''
1:注册
2:登录
3:购物
4:购物车
5:消费
'''
func_choice={
'1':register,
'2':login,
'3':shopping,
'4':shopping_car,
'5':pay,
}
while True:
print(func_msg)
lc=input('请输入你想要选择的功能,退出请按q:')
if lc=='q':
break
else:
func_choice[lc]()