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 @   xuchaoxin1375  阅读(9)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示