模拟登录
模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
代码如下:
#!/usr/bin/env python3
username = input("username: ")
count = 0
flag = False
lock_list = []
with open("lock.txt", "r") as f:
lock_f = f.readlines()
for lock in lock_f:
lock_list.append(lock.strip("\n"))
if username in lock_list:
print("用户%s已被锁定,请联系管理员!" % username)
else:
while True:
count += 1
password = input("password: ")
with open("passwd.txt", "r") as open_passwd_file:
read_passwd_file = open_passwd_file.readlines()
for line in read_passwd_file:
user_pass = line.strip().split(":")
if username == user_pass[0] and password == user_pass[1]:
print("欢迎用户%s成功登录" % username)
flag = True
break
else:
continue
if flag is True:
break
else:
if count == 3:
lock_file = open("lock.txt", "a")
lock_file.write("%s\n" % username)
lock_file.close()
print("用户%s密码输入错了3次,此用户已被锁定,请联系管理!" % username)
break
注:运行代码前要创建lock.txt、passwd.txt文件:
cat passwd.txt
ww1:ww1!
ww2:ww2!
ww3:ww3!
浙公网安备 33010602011771号