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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」