python简单用户管理模拟
编辑一个脚本,简单模拟用户管理系统,使得其具有以下功能:
- 注册:若用户存在,直接报错”name 已经存在”,若用户不存在,将用户信息保存起来,显示”注册成功”;
- 登陆:若用户存在,判断密码是否正确,判断正确后输出”登陆成功”。若用户不存在,报错”name 不存在”;
- 注销:用户存在,验证密码,确认后删除用户信息;若不存在,报错”name 不存在”;
- 退出;
该脚本如下所示:
Account = {}
def User_Exist(username):
if username in Account:
return True
else:
return False
def Passwd_right(username, password):
if password == Account.get(username):
return True
else:
return False
def Create():
while True:
print "Please input username and password what you want to create !"
New_user = raw_input("Username : ")
if User_Exist(New_user):
print "user is exist ! please input again !"
else:
break
print "\n"
New_password = raw_input("Password :")
Account.setdefault(New_user, New_password)
return "Account was created successfully !"
def Login():
a = 0
while a < 3:
print "Please input your username and password !"
Username = raw_input("Username : ")
Password = raw_input("Password : ")
if User_Exist(Username):
if Passwd_right(Username, Password):
return "Login successfully !"
else:
print "Your username or password wrong !"
else:
print "This username is not exist !"
a += 1
print "\n"
else:
return "The number of faults is bigger than 3 !"
def Close():
a = 0
while a < 3:
print "Please input username and password what you want to close !"
Close_username = raw_input("Username : ")
Close_password = raw_input("Password : ")
if User_Exist(Close_username):
if Passwd_right(Close_username, Close_password):
Right = raw_input("Do you really want to close user this account ?(Y/N) ")
if Right in "Yy":
Account.pop(Close_username)
return "Your account close successfully"
elif Right in "Nn":
return "Select None !"
else:
print "Please input Y or N !"
print "\n"
else:
print "Your username or password wrong !"
print "\n"
else:
print "This username is not exist !"
print "\n"
a += 1
else:
return "The number of faults is bigger than 3 !"
def Quit():
print "Goodbye !"
print "\n"
return exit(0)
Select_dict = {
"1": Create,
"2": Login,
"3": Close,
"4": Quit
}
while True:
print "\n"
print """
welcome to xxx system
There is some function you can select :
1. Create Account
2. Login
3. Close the Account
4. Quit
"""
Select = raw_input("Please input your select (1,2,3,4) :").strip()
if Select in Select_dict.keys():
print Select_dict[Select]()
else:
print "Please input right select (1,2,3,4) !"
该脚本运行后的效果图如下图所示:
用户注册的效果图:
注册时可以看出,当注册成功后,显示用户创建成功。当用户存在时,显示用户已存在。
登陆时的效果图:
由登陆效果图可以看出,当用户登录时,用户不存在,显示用户不存在。当用户密码错误时,显示用户账号或密码错误。当错误次数超过三次时,显示失败的次数超过三次,然后退出登陆界面。当用户输入正确的账号密码时,显示登陆成功。
用户注销的效果图:
注销用户时,输入用户的账号和密码,选择是否确定删除这个用户,确认后删除这个用户,提示用户删除成功。
退出的效果图,选择后直接退出系统: