用户登录程序
任务内容
- 1、输入用户名和密码
- 2、认证成功后显示欢迎信息
- 3、输错3次后锁定
流程图
![](https://images2017.cnblogs.com/blog/1021348/201708/1021348-20170831133233468-844995180.png)
代码
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)
root rootgm
gm 12345
hlr hlr123
gm
测试结果
![](https://images2017.cnblogs.com/blog/1021348/201708/1021348-20170831140955952-713885632.png)
![](https://images2017.cnblogs.com/blog/1021348/201708/1021348-20170831141014608-242135062.png)
![](https://images2017.cnblogs.com/blog/1021348/201708/1021348-20170831141022155-483906400.png)
![](https://images2017.cnblogs.com/blog/1021348/201708/1021348-20170831141229077-1158205563.png)
![](https://images2017.cnblogs.com/blog/1021348/201708/1021348-20170831141239593-1752235951.png)