day1 job: writing landing interface

Keywords: Python Database

Assignment 1: Write the login interface

1. Enter username and password

2. Display welcome information after successful certification

3. Lock-in after three wrong transfers

Thought: Requirement is to write the login interface, then there must be a module to store user information; three times after locking, there must be a module to store locked user information; We know that there are two ways to save user information, one is database storage, the other is file storage, now we only learn file storage, so there must be two files, one is user information. One is to lock the user information file.

To read information from files, add information and modify information.

    readme:

(1) User input username;

(2) User name to lock the file to verify whether the lock; if the lock let the user contact the administrator to solve;

(3) If the user file is not locked to verify the existence of the user, there exists a password for the user to enter and display welcome information after successful input; after three mistakes, the user is locked;

(4) If the user is not in the user file, prompt the user not to register, need to register, write the registration module, let the user register, add the user to the user list after registration, and display welcome information, do not let the user input, automatically link to the login status.

The flow chart is as follows:

 

 

active = None
#Identifier of Program Execution

def verification(username):
    """Verify that the user is locked"""
    locked_users = []
    with open("locked_file") as locked_f:
        for line in locked_f:
            locked_user,locked_pwd = line.split(":")
            locked_users.append(locked_user)
        print(locked_users)
    if username in locked_users:
        print("Sorry, your username has been locked, please contact the administrator!")
    else:
        active = True
        return active

def is_registered(username):
    """Verify whether the user is registered or not, registration allows the user to log in, and unregistered allows the user to choose to re-enter or register."""
    users = {}
    with open("active_file","r+") as f:
        for active_line in f:
            user,pwd = active_line.split(":")
            users[user] = pwd

    if username in users.keys():
        test_num = 0
        while test_num < 3:
            user_pwd = input("Please enter your password:")
            if user_pwd == users[username]:
                print("welcome back,have a good time!")
                return False
            else:
                test_num += 1
        else:
            #User input more than three times to lock the user, using while...else...Method
            print("Sorry, you entered too many times, your user has been locked, please contact the administrator!")
            mes = "\n" + username + ":" + users[username]
            with open("locked_file","a") as f1:
                f1.write(mes)
            return False
    else:
        print("The user name you entered does not exist, please follow the following tips to select!")
        print("Registration: Please enter 1\n Login: Please enter 2")
        num = input("Please enter your choice:")
        if num == "1":
            register_name = input("Please enter the username you want to register:")
            while True:
                register_pwd = input("Please enter your password:")
                register_pwd2 = input("Please enter your password again:")
                if register_pwd == register_pwd2:
                    break
                else:
                    print("The password you entered is incorrect. Please re-enter it.")
            message = "\n" + register_name + ":" + register_pwd
            print("Thank for your registing,have a good time!")
            with open("active_file","a") as f_obj:
                f_obj.write(message)
            return False

        elif num == "2":
            user_name = input("Please re-enter your username:")
            verification(user_name)

if __name__ == "__main__":

    username = input("Please enter your username:")
    active = verification(username)
    while active:
        active = is_registered(username)

The above code realizes the function of verification and locking, and also realizes the registration function of new users, but there is a drawback, that is, when the user is locked, it is not deleted from the current file. Adding information to the file of locked user information is very simple, but deleting an information from the file is not very simple and can not be as casual as the list. The desired operation. You can only convert first, and when you manipulate files in the list, line breaks and so on are disgusting. The above code will be converted as follows, learning is a tedious thing, but also to do the perfect, the following will refer to the code written by others, learn how others do not need to delete information in the file, and then improve the above code.

Posted by SoccerGloves on Tue, 25 Jun 2019 12:48:20 -0700