购物车
msg="""
1 注册
2 登录
3 注销
4 购物
5 购物车
6 退出
"""
prize_dict={
'1': "商品1",
'2': "商品2",
'3': "商品3",
}
username_list=[]
prize_dicts={}
def username_pwd_func():
username=input("请输入用户名:").strip()
pwd=input("请输入密码:").strip()
return username,pwd
def register():
username_inp,pwd_inp=username_pwd_func()
with open("user_info.txt",'a',encoding='utf-8') as fa:
fa.write(f"{username_inp}:{pwd_inp}\n")
print (f"{username_inp}注册成功")
def login():
count=0
while count<3:
username, pwd = username_pwd_func()
user_info = f"{username}:{pwd}"
with open('user_info.txt', 'r', encoding='utf-8') as fr:
data = fr.read().split()
if user_info in data:
print(f"{username}登陆成功")
username_list.append(username)
return
print ("登陆失败")
count+=1
def logout():
if not username_list:
print ("请首先登陆,才可以使用该功能")
return
username_list.clear()
def shopping():
if not username_list:
print ("请首先登陆,才可以使用该功能")
return
print (prize_dict)
prize_choice=input("请输入你选择的商品序列号")
if prize_choice not in prize_dict:
print ("你输入的商品不存在")
return
prize=prize_dict[prize_choice]
if prize not in prize_dicts:
prize_dicts[prize]=1
else:
prize_dicts[prize]+=1
print (prize_dicts)
def shopping_car():
if not username_list:
print ("先登陆,才可以使用该功能")
return
print(f"购物成功:{prize_dicts}")
prize_dicts.clear()
func_dict={
'1':register,
'2':login,
'3':logout,
'4':shopping,
'5':shopping_car,
}
print(msg)
while True:
choice=input("请输入你的功能选择,按q退出")
if choice=='q':
break
if choice not in func_dict:
print ("你输入的功能序号有误,请确认后输入")
continue
func_dict[choice]()