python实训案例-用户账户管理
描述 | 具体 |
语言 | python |
题目描述 | 某些网站要求访问者在访问网站内容之前必须先进行登录,若访问者没有该网站的账号,则需要先进行注册。访问者注册完账号后,网站的服务器会保存账号信息,以便访问者下次访问网站可根据保存的信息验证访问者身份。未保障账户安全,访问者可时常修改账户的密码; |
要求 | 注册,登录,修改密码,注销账号 |
# 用户类
class User:
name = ""
password = ""
# Dao层数据类
# 用于和文件进行交互
class DaoDate:
# 注册函数
def Register(self, user):
userId = user.name
userPassword = user.password
with open('userFile.txt', 'r') as file:
userDate = file.readline()
while userDate:
if userDate.split('~')[0] == userId:
return False
userDate = file.readline()
pass
with open('userFile.txt', 'a+') as file:
line = userId + "~" + userPassword
file.writelines(line + "\n")
return True
pass
# 登录函数
def Login(self, user):
with open('userFile.txt', 'r') as file:
message = file.readline()
while message:
Date_name = message.split('~')[0]
Date_pass = message.split('~')[1]
if Date_name == user.name:
if Date_pass == user.password + "\n":
return True
message = file.readline()
return False
# 修改密码
def upPassword(self, user):
lineList = []
with open('userFile.txt', 'r') as file:
lineList = file.readlines()
for mes in lineList:
if user.name == mes.split("~")[0]:
lineList.remove(mes)
lineList.append(user.name + "~" + user.password + "\n")
break
pass
with open('userFile.txt', 'w') as file:
pass
with open('userFile.txt', 'w') as file:
for i in lineList:
file.writelines(i)
pass
def Zhuxiao(self, us):
with open('userFile.txt', 'r') as ff:
lineList = ff.readlines()
for mes in lineList:
if us.name == mes.split("~")[0]:
lineList.remove(mes)
break
pass
with open('userFile.txt', 'w') as file:
pass
with open('userFile.txt', 'w') as file:
for i in lineList:
file.writelines(i)
pass
# u = User()
# u.name = "123q"
# u.password = "45kdwjcner6"
# d = DaoDate()
# print(d.Login(u))
# d.upPassword(u)
# 展示界面
def View():
print(" ------欢迎来到登录界面------")
print("-" * 30)
print("|", " " * 11, "1.注册", " " * 8, "|")
print("|", " " * 11, "2.登录", " " * 8, "|")
print("|", " " * 11, "3.修密", " " * 8, "|")
print("|", " " * 11, "4.注销", " " * 8, "|")
print("|", " " * 11, "5.退出", " " * 8, "|")
print("-" * 30)
View()
flag = False
fun = int(input("请输入你的操作:"))
while fun != 5:
if fun == 1: # 进到注册界面
UserId = str(input("请输入你的账号:"))
UserPassword = str(input("请输入你的密码:"))
u = User()
u.name = UserId
u.password = UserPassword
d = DaoDate()
boo = d.Register(u)
if boo:
print("注册成功!")
else:
print("注册失败!")
elif fun == 2: # 进到登录界面
Login_UserId = input("请输入你的账号:")
Login_UserPassword = input("请输入你的密码:")
u = User()
u.name = Login_UserId
u.password = Login_UserPassword
d = DaoDate()
boo = d.Login(u)
if boo:
print("登录成功!")
else:
print("登录失败!")
elif fun == 3: # 进到修改密码阶段
Test_UserId = input("请输入你的原始账号:")
Test_UserPassword = input("请输入你的原始密码:")
u = User()
u.name = Test_UserId
u.password = Test_UserPassword
d = DaoDate()
boo = d.Login(u)
if boo:
newPassword_1 = input("请输入你的新密码:")
newPassword_2 = input("请再次输入你的新密码:")
while newPassword_1 != newPassword_2:
print("两次密码不一致,请重新输入!")
newPassword_1 = input("请输入你的新密码:")
newPassword_2 = input("请再次输入你的新密码:")
u.password = newPassword_1
d.upPassword(u)
else:
print("登录失败!")
elif fun == 4:
Test_UserId = input("请输入你要注销的账号:")
Test_UserPassword = input("请输入你要注销的密码:")
us = User()
us.name = Test_UserId
us.password = Test_UserPassword
d = DaoDate()
boo = d.Login(us)
if boo:
d.Zhuxiao(us)
print("注销成功")
else:
print("注销失败!")
else:
print("输入无效,请重新输入!")
fun = int(input("请输入你的操作:"))