参考代码如下:
1.用户登录程序流程控制代码:
1
#
编辑者:闫龙
2
if
__name__ == ‘__main__‘:
3import UserLoginFuncation
4 LoclCount=[];
5while True:
6 UserName = input("用户名:>>")
7if(UserLoginFuncation.CheckUserLock(UserName)):
8print("用户",UserName,"已被锁定")
9continue10 PassWd = input("密 码:>>")
11if(UserLoginFuncation.UserInfo(UserName,PassWd)):
12print("欢迎",UserName,"登陆")
13break14else:
15 LoclCount.append(UserName);
16print("用户名或密码错误,您还有",3-LoclCount.count(UserName),"次尝试机会")
17if(LoclCount.count(UserName) == 3):
18 UserLoginFuncation.LockUser(UserName)
19print("对不起,尝试次数过多",UserName,"用户已被锁定")
2.用户登录程序调用函数(加入装饰器):
1
#
编辑者:闫龙
2
import
UserLoginDeco
3
import
os
4 @UserLoginDeco.Jude(2)
5def CheckUserLock(UserName):
6if(os.path.exists("LockUser")):
7 with open("LockUser","r",encoding="utf8") as ReadLock:
8 p = ReadLock.readline().split(",")
9if(p.count(UserName) != 0):
10return True
11else:
12return False
1314 @UserLoginDeco.Jude(3)
15def LockUser(UserName):
16if(os.path.exists("LockUser")):
17 LockFile = open("LockUser", "a", encoding="utf8")
18 LockFile.write(UserName+",")
19 LockFile.close()
20else:
21 LockFile = open("LockUser", "w", encoding="utf8")
22 LockFile.write(UserName+",")
23 LockFile.close()
2425 @UserLoginDeco.Jude(1)
26def UserInfo(UserName,PassWd):
27 with open("UserInfo",mode="r",encoding="utf8") as userinfo:
28 p = userinfo.readlines()
29for i in p:
30 i= i.strip().split(":")
31if(i[0] == UserName and i[1] == PassWd):
32return True
33else:
34continue35return False
3.用户登录程序装饰器代码:
1
#
编辑者:闫龙
2
import
time
3
def
Jude(InputType):
4
def
ViewUser(UserLoginFunc):
5
def PrintUser(*args):
6if(InputType == 1):
7while True:
8print("您输入的用户名是",args[0],"密码是",args[1])
9 choice = input("请问是否继续登陆(y/n)")
10if (choice.lower() == "y"):
11 res = UserLoginFunc(args[0],args[1])
12return res
13elif(choice.lower() == "n"):
14break;
15else:
16print("您的选择有误")
17continue18if(InputType == 2):
19print("正在比对用户",args[0],"的信息......")
20 time.sleep(2)
21 res = UserLoginFunc(args[0])
22return res
23if(InputType == 3):
24print("正在锁定",args[0],"用户")
25 time.sleep(2)
26 res = UserLoginFunc(args[0])
27return res
28return PrintUser
29return ViewUser
4.用户登录文件(UserInfo):
1 egon:123
2 alex:321
3 long:666
原文:http://www.cnblogs.com/DragonFire/p/6691558.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!