用python的文件操作写一个关于用户账户管理,有用户,密码,身份,状态的详细代码,不用面向对象
时间: 2024-05-24 10:10:19 浏览: 24
# 读取用户信息文件
with open("users.txt", "r") as f:
users = f.readlines()
# 定义用户信息字典
user_info = {}
# 解析用户信息
for user in users:
user = user.strip().split(",")
username = user[0]
password = user[1]
identity = user[2]
status = user[3]
user_info[username] = {"password": password, "identity": identity, "status": status}
# 注册新用户
def register():
username = input("请输入用户名:")
if username in user_info:
print("该用户名已存在!")
return
password = input("请输入密码:")
identity = input("请输入身份(admin/user):")
status = "normal"
user_info[username] = {"password": password, "identity": identity, "status": status}
with open("users.txt", "a") as f:
f.write(f"\n{username},{password},{identity},{status}")
print("注册成功!")
# 登录
def login():
username = input("请输入用户名:")
if username not in user_info:
print("该用户名不存在!")
return
password = input("请输入密码:")
if password != user_info[username]["password"]:
print("密码错误!")
return
if user_info[username]["status"] == "locked":
print("该用户已被锁定!")
return
if user_info[username]["identity"] == "admin":
print("管理员登录成功!")
# 管理员操作
else:
print("用户登录成功!")
# 用户操作
# 锁定用户
def lock_user():
username = input("请输入要锁定的用户名:")
if username not in user_info:
print("该用户名不存在!")
return
user_info[username]["status"] = "locked"
with open("users.txt", "w") as f:
for username, info in user_info.items():
password = info["password"]
identity = info["identity"]
status = info["status"]
f.write(f"{username},{password},{identity},{status}\n")
print(f"{username}已被锁定!")
# 解锁用户
def unlock_user():
username = input("请输入要解锁的用户名:")
if username not in user_info:
print("该用户名不存在!")
return
user_info[username]["status"] = "normal"
with open("users.txt", "w") as f:
for username, info in user_info.items():
password = info["password"]
identity = info["identity"]
status = info["status"]
f.write(f"{username},{password},{identity},{status}\n")
print(f"{username}已被解锁!")
# 删除用户
def delete_user():
username = input("请输入要删除的用户名:")
if username not in user_info:
print("该用户名不存在!")
return
del user_info[username]
with open("users.txt", "w") as f:
for username, info in user_info.items():
password = info["password"]
identity = info["identity"]
status = info["status"]
f.write(f"{username},{password},{identity},{status}\n")
print(f"{username}已被删除!")
# 显示所有用户信息
def show_all_users():
print("用户名\t\t密码\t\t身份\t状态")
for username, info in user_info.items():
password = info["password"]
identity = info["identity"]
status = info["status"]
print(f"{username}\t\t{password}\t\t{identity}\t{status}")
# 主函数
def main():
while True:
print("=" * 30)
print("欢迎使用用户账户管理系统")
print("1. 注册新用户")
print("2. 登录")
print("3. 锁定用户")
print("4. 解锁用户")
print("5. 删除用户")
print("6. 显示所有用户信息")
print("0. 退出")
print("=" * 30)
choice = input("请输入操作编号:")
if choice == "1":
register()
elif choice == "2":
login()
elif choice == "3":
lock_user()
elif choice == "4":
unlock_user()
elif choice == "5":
delete_user()
elif choice == "6":
show_all_users()
elif choice == "0":
print("再见!")
break
else:
print("输入有误,请重新输入!")
if __name__ == "__main__":
main()
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)