Pythone实现用户注册和登录,包括3次输入错误的账户锁定

创建2个txt格式文件,user.txt和userlock.txt
注册的时候将用户名和密码写入user.txt
一行用户名,一行密码,

在登录时,进入user.txt按行查找用户名和密码,找到用户名后用flag标记为1,
下一行如果与密码匹配则跳出循环,不匹配清除flag重新输入密码,避免跳行查找密码

如果三次登录失败,则将此时的用户名添加到userlock.txt,下次登录的时候遍历userlock.txt
如果匹配跳出循环不再输入密码

file = open(r'E:\PythoneDATA\Firtday\user.txt','w+')

while True:
    choose = int(input("choose to select "
                   "\n1.sign up"
                   "\n2.sign in"
                   "\n0.exit"
                   "\n"))
    if choose==1:
        _username = input("username:")
        while True:
            _password1=input("password:")
            _password2=input("enter password again:")
            if(_password1!=_password2):
                print("your password isn't currect"
                      "please enter again")
            else:
                print("successfully sign up ")
                break
        with open(r'E:\PythoneDATA\Firtday\user.txt','w+') as file:
            file.write(_username)
            file.write('\n'+_password1+'\n')
        file.close()
    if choose==2:
        file = open(r'E:\PythoneDATA\Firtday\user.txt', 'r')
        print('''
         ------------------------------------------
               Welcome,please enter your
                 username and password
         ------------------------------------------''')
        sign_in=0
        for i in range(3,0,-1):
            username = input('           username:')
            password = input('           passward:')
            flag=0
            for i, line in enumerate(file2):
                if username== line:
                    print("this account has been locked")
                    break;
                for i, line in enumerate(file):
                    if line==username:
                        flag+=1
                        continue
                    if flag==1 and line==password:
                        print('succeessfully sign in')
                        sign_in=1
                        break
                    if flag==1 and line!=password:
                        flag-=1
                    if flag<0:
                        print("username or password error")
                        sign_in=0
                        break
                if sign_in==0:
                    print("you have {0} times to try".format(i))
        if i>=3:
            with open(r'E:\PythoneDATA\Firtday\userlock.txt', 'w+') as file2:
                file2.write(username)
            print("this account has been locked")
    if choose==0:
        break

你可能感兴趣的:(Pythone实现用户注册和登录,包括3次输入错误的账户锁定)