用户管理系统实现持久化
import sys
import hashlib
# 用来存储所有的用户信息
users = []
slat = "lx"
def password_by_md5(password):
md5 = hashlib.md5(password.encode("utf-8"))
# 盐值
md5.update(slat.encode("utf-8"))
return md5.hexdigest()
def main():
print("~*" * 20)
print("")
print("\t\t 1、用户注册\n")
print("\t\t 2、用户登录\n")
print("\t\t 3、退出系统\n")
print("~*" * 20)
choice = input("请输入您要操作的选项:")
return choice
def exists_user(username):
for i in users:
if i.get("username") == username:
return True
return False
def is_login(username, password):
for i in users:
if i.get("username") == username and i.get("password") == password:
print("登录成功")
return True
return False
def register():
username = input("请输入您的用户名称:")
password = input("请输入您的用户密码")
# 校验数据
if username is None or username.strip() == "":
print("对不起,用户名不能为空")
return
if password is None or password.strip() == "" or len(password) < 3:
print("对不起,密码长度不能少于3位")
return
# 判断该用户是否存在
if exists_user(username):
print("对不起,该用户已经存在,请重新输入!!")
return
# 组建成一个字典对象
user = {"username": username, "password": password_by_md5(password)}
users.append(user)
print(users)
def login():
username = input("请输入您的用户名称:")
password = input("请输入您的用户密码")
# 加密密码
password = password_by_md5(password)
if is_login(username, password):
print("恭喜你,登录成功")
else:
print("对不起,登录失败,请重新登录")
while True:
choice = main()
if choice == "1":
register()
elif choice == "2":
login()
else:
print("程序正常退出")
sys.exit(0)