Python million annual salary of the road - the second week - a cubic account login lock function

Achieve three user login and locking (elected to do, plenty of time to make a recommendation to do) with the code
project analysis:
A first program starts, it displays the following contents for users to choose:
1. Registration
2. Log

a. When the user selects a login, first determine the user name exists not exist can not be registered in userinfo.txt table
b. When the registered user name does not exist in the user name and password is written to the file userinfo.txt
c the user chooses to log time, determine unanimously whether the user input account number and password userinfo.txt storage
d. username and password an agreement on the termination of the cycle, and prompts the user to log on success!
E. inconsistent user name and password, only three login opportunities, three times after prompt the user name is locked, contact the administrator! and terminate the cycle
f. when the user name output three times, run the program again. login lockout continues to prompt the user name of the account is locked, please contact the administrator!

import os
count = 0
dic = {}
def locked(login_user,count = 3):
    with open("userinfo.txt", "r", encoding="UTF-8") as f4, open("userinfo_bak.txt", "w", encoding="UTF-8") as f5:
        for i in f4:
            i = i.strip().split(":")
            if i[0] == login_user:
                i[0] = i[0].replace(i[0], i[0] + "$")
            f5.write(f"{i[0]}:{i[1]}\n")
    # print(f"你输错了{count}次!")
    print(f"检测到用户名{login_user}已经输错了{count}次,已锁定!请联系管理员!")
    os.remove("userinfo.txt")
    os.rename("userinfo_bak.txt", "userinfo.txt")
def user_login_count(login_user):
    with open("userinfo.txt", "r", encoding="UTF-8") as f:
        for i in f:
            i = i.strip().split(":")
            if i[0][-1] == "$" and i[0][0:-1] == login_user:
                dic[login_user] = 3
    if dic.get(login_user,0) != 3:
        dic[login_user] = dic.get(login_user,0) + 1
    # print(dic[login_user])
    return dic[login_user]
def register():
    user = input("请输入注册用户名(退出程序请按q/Q):")
    if user.lower() == "q":
        return "q"
    while 1:
        pwd = input("请输入注册密码:")
        if not pwd.strip():
            print("注册密码不能为空!请重新输入!")
        else:
            break
    with open("userinfo.txt", "r", encoding="UTF-8") as f:
        flag = True
        for i in f:
            i = i.split(":")
            if user == i[0]:
                print("用户名已存在!")
                flag = False
        if flag == True:
            with open("userinfo.txt", "a", encoding="UTF-8") as f1:
                f1.write(user + ":" + pwd + "\n")
                print("注册成功!")
def login():
    flag = False
    while 1:
        login_user = input("请输入登录用户名(退出程序请按q/Q):")
        if login_user.lower() == "q":
            return "q"
        login_pwd = input("请输入登录密码:")
        with open("userinfo.txt","r",encoding="UTF-8") as f:
            for i in f:
                i = i.strip().split(":")
                if i[0][-1] == "$" and i[0][0:-1] == login_user:
                    print(f"用户名{login_user}已被锁!")
                    break
                elif i[0] == login_user and i[1] == login_pwd:
                    flag = True
                    break
        if flag != True:
            ret = user_login_count(login_user)
            if ret == 3:
                locked(login_user)
            else:
                print(f"登陆失败,你已经输错{ret}次了!")
        else:
            print("登陆成功!")
            dic[login_user] = 0
            break
while 1:
    inp = input("请输入序号(1/注册,2/登录,q或Q/退出程序):")
    if inp == "1" or inp == "2" or inp.lower() == "q":
        if inp == "1":
            if register() == "q":
                break
        elif inp == "2":
            if login() == "q":
                break
        elif inp.lower() == "q":
            print("已退出程序!")
            break
    else:
        print("请重新输入!")

Guess you like

Origin www.cnblogs.com/zhangchaoyin/p/11221197.html