python_系统登录模拟。先手工制作一个文本文件‘account.txt’,内容包含以下3位用户的账号和密码

文章目录

problem:

然后,通过input()输入某位用户的账号和密码进行系统登录的模拟,要求将每次登录的信息记录在一个日志文件”log.txt”中,包含输入的账号与登录的具体时间,中间用逗号隔开。
另外,如果输入的账号和密码不与‘account.txt’文件中的任何一个相同,利用print()函数给出提示:“您的账号或密码有误!”;
如果连续五次登录失败,提示:“您的账号将被锁定!”。

result:

在这里插入图片描述

code:

def get_formatted_time():
    return (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))


account, pwd = "", ""

items_list = []


def get_accounts_existed():
    with open(path_string_fix+"account.txt", "r") as file_input_stream:
        for item in file_input_stream:
            items_list.append(item.strip().split(":"))
# print(items_list)


def log_record(account,pwd):
    with open(path_string_fix+"log.txt", "a") as file_output_stream:
        file_output_stream.write(account+","+pwd+",time:"+get_formatted_time()+"\n")


def login():
    for i in range(5):
        get_accounts_existed()
        account, pwd = (input("input account:")), (input("input password:"))
        log_record(account,pwd)
        if [account, pwd] in items_list:
            print("welcome! "+account)
            return
        else:
            print("您的账号或密码有误!")
        # if i==4:
        #     print("您的账号将被锁定!")
    print("您的账号将被锁定!")

login()

posted @ 2021-04-08 13:08  xuchaoxin1375  阅读(20)  评论(0)    收藏  举报  来源