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()