用户登录程序
任务内容
- 1、输入用户名和密码
- 2、认证成功后显示欢迎信息
- 3、输错3次后锁定
流程图
代码
- 1、主文件
import sys
lock = "lock.txt"
logfile = "login.txt"
login_info = 0
i = 0
while i < 3 and login_info== 0 :
name = input("Please input your name: ")
f = open(lock, "r")
for line in f.readlines():
# if name in line:
if name == line.strip():
f.close()
sys.exit('\033[32:1m用户 %s 已经被锁定\033[0m' % name)
password = input("Please input password: ")
f = open(logfile, "r")
for line in f.readlines():
user_file, pass_file = line.split()
if user_file == name and pass_file == password:
print("Bingo!")
login_info = 1
break
else:
print("You name or password is errer!")
i += 1
f.close()
else:
if i == 3 and login_info == 0:
f = open(lock, "a")
f.write(name + "\n")
f.close()
print('\033[32:1m用户 %s 已经被锁定\033[0m' % name)
- 2、系统用户帐号密码信息
root rootgm
gm 12345
hlr hlr123
- 3、当前系统被锁用户信息
gm
测试结果
- 1、测试锁定用户能否登录系统
- 2、测试登录3次能否登录
- 3、测试登录1次能否登录
- 4、测试登录3次失败后,是否会锁定帐号