python editing user login interface

1. Demand analysis

The login interface shall meet the following requirements:

  1. The system has two options: login and registration
  2. The system should be able to realize the login error prompt, such as the account password error, etc., and the user information should be saved in the user_info.txt folder
  3. The system shall be able to count the number of login errors and lock them. When the user enters the account password three times in error, the changed account is locked, and the locked account is saved in the login? Lock.txt folder
  4. The registration interface should be able to prompt the user name repeatedly. When the user's registered account already exists, prompt the corresponding information.

 

1. Code implementation

getNum = int(input("1.Log in\n2.register\nPlease Input the Choose :"))
while getNum<1 or getNum>2:
    getNum =int(input("invalid value :"))

username = input("User name: ")
password = input("Password: ")

if getNum == 1:#Log in
    ErrNums = 0
    while ErrNums<3:
        T = False
        fp = open('login_lock.txt','r')#Open the lock file to see if the entered account is locked
        lines = fp.readlines()#Read all rows
        while T== False:
            for line in lines:
                if line.split(',')[0] == username:
                    print("Account locked !")
                    username = input("User name: ")
                    password = input("Password: ")
                    break
            T=True
        fp.close()

        T = False
        fp_user = open('user_info.txt','r')#To open a user information file:
        lines = fp_user.readlines()
        for line in lines:
            if line.split(',')[0] == username:
                if line.split(',')[1] == password+'\n'or line.split(',')[1] == password:
                    print("Login successfully !")
                    T = True
                    break
        fp_user.close()

        if T == False:#Login failed
            ErrNums += 1
            if ErrNums >= 3:
                fp_user = open('login_lock.txt','a')
                print("Account locked !")
                fp_user.write('\n'+username)
                fp_user.close()
                break
            print("Wrong account name or password !")
            username = input("User name: ")
            password = input("Password: ")
        else:
            break
elif getNum == 2:#register
    fp= open('user_info.txt','r')#To open a user information file:
    T = False
    lines = fp.readlines()  # Read all rows
    while T == False:
        for line in lines:
            if line.split(',')[0] == username:
                print("Account number already exists, please re-enter !")
                username = input("User name: ")
                password = input("Password: ")
                break
            T = True
    fp.close()
    fp = open('user_info.txt', 'a')  # To open a user information file:
    fp.write(username+','+password+'\n')
    print("login was successful !")

The contents of the user? Info.txt file are as follows:

    

The important parts of the code are annotated. At present, the creation of the txt file is stored in the project directory. It is convenient to use the relative path. If it is not in the folder, the path in the code needs to be changed to an absolute path, otherwise there will be problems after the program runs. There are still imperfections in the code. Forget to point out and discuss them with experienced friends.

Tags: Python

Posted on Thu, 13 Feb 2020 11:11:12 -0800 by dare87