'''
用函数完成登录注册以及购物车的功能
作业需求:
1,启动程序,用户可选择四个选项:登录,注册,购物,退出。
2,用户注册,用户名不能重复,注册成功之后,用户名密码记录到文件中。
3,用户登录,用户名密码从文件中读取,进行三次验证,验证不成功则退出整个程序。
4,用户登录成功之后才能选择购物功能进行购物,购物功能(就是将购物车封装到购物的函数中)。
5,退出则是退出整个程序。
'''
'''
分析:将用户信息保存到文件userinfo.txt(不考虑多个账号同时使用)
存储格式为{"alex":{"pwd":"123456", "status":"logged", "money": 8000, "shopping":{}},
"wusir":{"pwd": "123456", "status":"logout","money": 6000, "shopping":{}},
......
}
'''
#判断文件是否存在,不存在,则创建空文件
def mk_file():
if not os.path.exists(filename):
f = open(filename, mode="w", encoding="utf-8")
f.close()
#获取文件中的内容,并将内容转换为dict类型
def get_file_content():
usr_dic = {}
with open(filename, mode="r", encoding="utf-8") as f:
content = f.read().strip()
if content != "":
usr_dic = eval(content)
return usr_dic
#修改文件内容
def set_file_content(usr_dic):
with open("%s.bak" % filename, mode="w", encoding="utf-8") as f:
f.write(str(usr_dic))
os.remove(filename)
os.rename("%s.bak" % filename, filename)
#用户注册
def register():
while 1:
usr = input("Register Username: ").strip()
pwd = input("Register Password: ").strip()
if usr != "" and pwd != "": # 判断用户名和密码是否合法
if len(usr) < 3 or len(usr) > 30:
print("\033[31;0m用户名的长度必须在3~30.\033[0m")
continue
if len(pwd) < 6 or len(pwd) > 20:
print("\033[31;0m密码的长度必须在6~20.\033[0m")
continue
mk_file() # 判断文件是否存在,不存在直接创建空文件
usr_dic = get_file_content() #读文件,判断用户是否存在,存在则提醒已存在,不存在则直接添加
if usr in usr_dic:
print("\033[31;0m注册失败, 用户名: %s已存在!\033[0m" % usr)
return
else:
usr_dic[usr] = {"pwd": pwd}
set_file_content(usr_dic)
print("\033[32;0m恭喜您,用户名: %s注册成功!\033[0m" % usr)
return True
else:
print("\033[31;0m用户名和密码不能为空!\033[0m")
#用户登录
def login():
while 1:
usr = input("Login Username: ").strip()
pwd = input("Login Password: ").strip()
if usr != "" and pwd != "": ## 判断用户名和密码是否为空
mk_file() #判断文件是否存在,不存在则创建空文件
usr_dic = get_file_content() #读文件,判断用户名和密码是否正确
if usr in usr_dic:
if usr_dic[usr]["pwd"] == pwd: #登录成功
return usr
#用户密或密码错误
return
else:
print("\033[31;0m用户名和密码不能为空.\033[0m")
#记录用户信息
def record_userinfo(usr, stat="logged", money=0, shopping={}):
usr_dic = get_file_content()
usr_dic[usr]["status"] = stat
usr_dic[usr]["money"] = usr_dic[usr].get("money", 0) + money
#更新用户已购买的商品信息
if shopping != {}:
sp_dic = usr_dic[usr].get("shopping", {})
new_dic = shopping
for k, v in sp_dic.items():
if k in shopping:
v[2] += shopping[k][2]
new_dic[k] = v
usr_dic[usr]["shopping"] = new_dic
set_file_content(str(usr_dic))
#购物功能
def shopping(usr):
shopping_lst = [("电脑", 2999), ("鼠标", 30), ("键盘", 80), ("音响", 888), ("耳机", 60)]
shopping_carts = {}
print("\033[32;0m欢迎来到淘宝电脑城\033[0m".center(50, "*"))
while 1:
money = input("请输入您要充值的金额(元): ").strip()
if money.isdigit():
money = int(money)
usr_dic = get_file_content().get(usr, {}) #获取账号余额
money_total = usr_dic.get("money", 0) + money
print("\033[32;0m恭喜您,成功充值%d元! 您的账号余额为%d元.\033[0m" % (money, money_total))
record_userinfo(usr,