通过读取文件简单的实现用户的注册、登陆、锁定
1、流程图:
2、代码:
import getpass #用户注册 logon = input("Please of logon.....Y/N:") if logon == "y": logonName = input("please you name:") logonPassword = getpass.getpass() username = "UPLoading.txt" with open(username,"a") as logonlife: logonlife.write("\n") logonlife.write(logonName) logonlife.write(" ") logonlife.write(logonPassword) #注册结束后是否登陆 landing = input("Please of landing.....Y/N:") if landing == "n": exit() #实现读取锁定文件 lockfile = "Lock.txt" with open(lockfile) as lfile: user = [] for user1 in lfile.readlines(): userLine = user1.strip().split() user.append(userLine) #实现读取登陆文件 filename = "UPLoading.txt" with open(filename) as file: dataMat = [] for line in file.readlines(): curLine=line.strip().split(" ") dataMat.append(curLine[0:2]) count = 1 info = False while count < 4: Username = input("Username:") Password = input("Password:") #判断是否被锁定 for j in range(0, len(user)): if Username == user[j][0]: print("Please get out!!!", user[j][0]) break info = False else: #判断是否输入正确 for i in range(0, len(dataMat)): if Username == dataMat[i][0] and Password == dataMat[i][1]: print("Welcome", dataMat[i][0], "!!!") break info = False else: info = True if info == False: break else: count += 1 else: #实现当三次都输入错误后将用户名锁定到Lock.txt文件 lockfile = "Lock.txt" with open(lockfile,"a") as Lfile: Lfile.write("\n") Lfile.write(Username)