[python]代码库
count = 0
# 定义一个字典存储用户名和密码
names = {}
# 定义一个列表存储用户名
name_exit = []
username = input("username:")
# 判断输入的用户的用户是否在用户名单中
with open("name_login.txt", 'r') as f:
while True:
line = f.readline().strip()
if not line:
break
else:
name = line.split(':')[0]
passwd = line.split(':')[1]
names[name] = passwd
for key in names:
name_exit.append(key)
# 判断用户名是否正确,不正确则继续输入
while username not in name_exit:
print("The account is not exit.Check it again.")
username = input("username:")
else:
with open("name_lock.txt", "r") as f:
lock_name = f.read()
# 判断用户名是否在锁定文件中,如果在就退出程序
if username == lock_name:
print("Sorry.Your account has been locked.")
exit()
else:
# 3次输入密码的机会
while count < 3:
passwd_input = input("password:")
# 判断用户名和密码是否有效
if passwd_input == names[username]:
print("Welcome!", username)
break
else:
print("Error,please try again.")
count += 1
# 如果3次输入密码错误,则将用户名添加到锁定文件
if count == 3:
with open("name_lock.txt", "w") as f:
f.write("%s" % username)
print("You have tried 3 times,and your account will be locked")
高级设计师
by: Python自学 发表于:2022-11-16 02:41:06 顶(1) | 踩(1) 回复
name_login.txt
user1:python
user2:Noh346
回复评论