Python练习-函数版-锁定三次登陆失败的用户

代码如下:

 1 # 编辑者:闫龙
 2 if __name__ == '__main__':
 3     import UserLoginFuncation
 4     
 5 LoclCount=[];
 6 while True:
 7     UserName = input("用户名:>>")
 8     if(UserLoginFuncation.CheckUserLock(UserName)):
 9         print("用户",UserName,"已被锁定")
10         continue
11     PassWd = input("密  码:>>")
12     if(UserLoginFuncation.UserInfo(UserName,PassWd)):
13         print("欢迎",UserName,"登陆")
14         break
15     else:
16         LoclCount.append(UserName);
17         print("用户名或密码错误,您还有",3-LoclCount.count(UserName),"次尝试机会")
18     if(LoclCount.count(UserName) == 3):
19         UserLoginFuncation.LockUser(UserName)
20         print("对不起,尝试次数过多",UserName,"用户已被锁定")
用户登录程序流程控制代码
 1 # 编辑者:闫龙
 2 import os
 3 def CheckUserLock(UserName):
 4     if(os.path.exists("LockUser")):
 5         with open("LockUser","r",encoding="utf8") as ReadLock:
 6             p = ReadLock.readline().split(",")
 7             if(p.count(UserName) != 0):
 8                 return True
 9 
10 def LockUser(UserName):
11     if(os.path.exists("LockUser")):
12         LockFile = open("LockUser", "a", encoding="utf8")
13         LockFile.write(UserName+",")
14         LockFile.close()
15     else:
16         LockFile = open("LockUser", "w", encoding="utf8")
17         LockFile.write(UserName+",")
18         LockFile.close()
19 
20 def UserInfo(UserName,PassWd):
21     with open("UserInfo",mode="r",encoding="utf8") as userinfo:
22         p =  userinfo.readlines()
23         for i in p:
24             i= i.strip().split(":")
25             if(i[0] == UserName and i[1] == PassWd):
26                 return True
27             else:
28                 continue
29         return False
用户登录程序调用函数
egon:123
alex:321
long:666
用户登录文件(UserInfo)

 

posted @ 2017-04-09 22:00  DragonFire  阅读(287)  评论(0编辑  收藏  举报