参考代码如下:
1.用户登录程序流程控制代码:
1 # 编辑者:闫龙
2 if __name__ == '__main__':
3 import UserLoginFuncation
4 LoclCount=[];
5 while True:
6 UserName = input("用户名:>>")
7 if(UserLoginFuncation.CheckUserLock(UserName)):
8 print("用户",UserName,"已被锁定")
9 continue
10 PassWd = input("密 码:>>")
11 if(UserLoginFuncation.UserInfo(UserName,PassWd)):
12 print("欢迎",UserName,"登陆")
13 break
14 else:
15 LoclCount.append(UserName);
16 print("用户名或密码错误,您还有",3-LoclCount.count(UserName),"次尝试机会")
17 if(LoclCount.count(UserName) == 3):
18 UserLoginFuncation.LockUser(UserName)
19 print("对不起,尝试次数过多",UserName,"用户已被锁定")
用户登录程序流程控制代码(UserLogin.py)
2.用户登录程序调用函数(加入装饰器):
1 # 编辑者:闫龙
2 import UserLoginDeco
3 import os
4 @UserLoginDeco.Jude(2)
5 def CheckUserLock(UserName):
6 if(os.path.exists("LockUser")):
7 with open("LockUser","r",encoding="utf8") as ReadLock:
8 p = ReadLock.readline().split(",")
9 if(p.count(UserName) != 0):
10 return True
11 else:
12 return False
13
14 @UserLoginDeco.Jude(3)
15 def LockUser(UserName):
16 if(os.path.exists("LockUser")):
17 LockFile = open("LockUser", "a", encoding="utf8")
18 LockFile.write(UserName+",")
19 LockFile.close()
20 else:
21 LockFile = open("LockUser", "w", encoding="utf8")
22 LockFile.write(UserName+",")
23 LockFile.close()
24
25 @UserLoginDeco.Jude(1)
26 def UserInfo(UserName,PassWd):
27 with open("UserInfo",mode="r",encoding="utf8") as userinfo:
28 p = userinfo.readlines()
29 for i in p:
30 i= i.strip().split(":")
31 if(i[0] == UserName and i[1] == PassWd):
32 return True
33 else:
34 continue
35 return False
用户登录程序调用函数(加入装饰器)UserLoginFuncation.py
3.用户登录程序装饰器代码:
1 # 编辑者:闫龙
2 import time
3 def Jude(InputType):
4 def ViewUser(UserLoginFunc):
5 def PrintUser(*args):
6 if(InputType == 1):
7 while True:
8 print("您输入的用户名是",args[0],"密码是",args[1])
9 choice = input("请问是否继续登陆(y/n)")
10 if (choice.lower() == "y"):
11 res = UserLoginFunc(args[0],args[1])
12 return res
13 elif(choice.lower() == "n"):
14 break;
15 else:
16 print("您的选择有误")
17 continue
18 if(InputType == 2):
19 print("正在比对用户",args[0],"的信息......")
20 time.sleep(2)
21 res = UserLoginFunc(args[0])
22 return res
23 if(InputType == 3):
24 print("正在锁定",args[0],"用户")
25 time.sleep(2)
26 res = UserLoginFunc(args[0])
27 return res
28 return PrintUser
29 return ViewUser
用户登录程序装饰器代码(UserLoginDeco.py)
4.用户登录文件(UserInfo):
1 egon:123
2 alex:321
3 long:666
用户登录文件(UserInfo)
发表评论