python1_1

Log in to write interfaces (txt file to store information)

----aims----

· Enter the user name and password
• After successful authentication to display a welcome message
• After unsuccessful attempts to lock

----Process----

1. Enter the user name and password
if the user registration 2. detect
whether a user is detected 3. Lock
4. Is detect the same user name and password and registration information
5. logged in, welcome tips

---- ---- constitution

Two files:
username_password.txt: used to store user names and passwords
lock_username.txt: lock used to store user list
function checks whether the user registration: check_username_isExist ()
to detect whether the user lock function: check_username_isLock ()
to check whether the password is correct function: check_password (wrong_times)
unsuccessful attempts to enter the write function of the lock list: write_lockusername ()

Code ---- ----

user_name = ""
password = ""
count = 0#登录循环
wrong_times= 0#输错密码次数

#------------------检查该用户是否注册-----------------
def check_username_isExist():
    isExist = 0#0为未注册;1为已注册
    with open(r"C:\Users\10524\PycharmProjects\untitled\t1\username_password.txt")as file1:#打开有用户名和密码的文件
        user_list = file1.readlines()#读取到user_list
        for List_user_pw in user_list:
            (user, password1) = List_user_pw.strip("\n").split(" ")#标注文件中用户名和密码
            if user_name == user:
                isExist = 1
    return isExist
    
#-----------------检查该用户是否被锁定------------------
def check_username_isLock():
    with open(r"C:\Users\10524\PycharmProjects\untitled\t1\lock_username.txt")as file2: #打开有用户被锁定名单的文件
        locklist = file2.readlines()#读取到lock_list
        for lock_name in locklist:
            username = lock_name.strip("\n")#标注文件中用户名
            if user_name == username:
                print("The user is locked!")
                exit()
    return

#------------------检查密码是否正确-----------------
def check_password(wrong_times):
    with open(r'C:\Users\10524\PycharmProjects\untitled\t1\username_password.txt')as file:#打开有用户名和密码的文件
        list = file.readlines()#读取到list
        for user in list:
            (username,passsword1) = user.strip("\n").split(" ")#标注文件中用户名和密码
            if user_name == username and password == passsword1:
                print("login successfully...")#登录成功
                exit()
            elif user_name == username and password != passsword1:
                wrong_times+=1#密码不一致,错误次数加一
                print("The password is error!")#登陆失败
    return wrong_times

#将输错次数达三次的用户名写入锁定文件
def write_lockusername():
    file = open("lock_username.txt", "a")
    file.write('''\n{a}'''.format(a = user_name))
    return

#------------------主程序-----------------
while count == 0:
    print("Welcome to login")
    user_name = input("username:")
    password = input("password:")#输入用户名和密码
    #ret =
    if check_username_isExist() == 0:#检查是否注册
        print("The username is not exist,Please try again.")
    else:
        check_username_isLock()#检查是否锁定
        wrong_times= check_password(wrong_times)#检查密码是否正确:错则累加wrong_times次数;对则成功登录
        if wrong_times== 3:
            write_lockusername()#达到三次进行锁定操作
            exit()

----test----

Create and pre-written username_password.txt: Here Insert Picture Description
created empty lock_username.txt file:
Here Insert Picture Description
test unregistered and registered:
Here Insert Picture Description
test the password is wrong and is locked


Here Insert Picture Description
The user is locked file appears:
Here Insert Picture Description

----End----

Released two original articles · won praise 1 · views 53

Guess you like

Origin blog.csdn.net/weixin_42943239/article/details/104054909