python如何重复输⼊密码_python循环和file操作实现⽤户密码
输错三次将⽤户锁定...
⼀、需求
编写登录接⼝
1.输⼊⽤户名密码
2.认证成功后显⽰欢迎信息
3.输错三次后锁定
⼆、简单思路
登录,三次密码输⼊错误锁定⽤户
1.⽤户信息⽂件:存放⽤户名和密码
2.⿊名单⽂件:将输⼊三次错误的⽤户加⼊此名单,加⼊此名单的⽤户将不允许登录
三、具体实现
第⼀步:在⿊名单⾥检测是否有此⽤户,如果有则不让登录
第⼆步:⽤户名和密码判定
代码如下:
#!/usr/bin/env python3
count = 0 #记录⽤户输⼊密码的次数
flag = 1 #标志位
lock = []
user_pass = []
username = input('please input your name:')
#读取⿊名单的内容
f = open('black_mingdan','r')
lock_file = f.readlines()
f.close()
#将⿊名单⽂件内容作为列表元素追加到列表中
for i in lock_file:
line = i.strip('\n')
lock.append(line)
#若输⼊的⽤户名在⿊名单中,如果在则给出提⽰信息:⽤户已经被锁定,请联系管理员。
if username in lock:
print('User %s Have Been Locked.It is not allow to login,please contact Administrator.' %username)
else:
#输⼊的⽤户名不在⿊名单中,则提⽰⽤户输⼊密码信息
while True:
count += 1
passwd = input("please input your password:")
f = open('user_information','r')
user_file = f.readlines()
f.close()
for i in user_file:
user_pass = i.strip().split()
#判断输⼊的⽤户名==user_pass[0] and 密码==user_pass[1],如果相等,则提⽰欢迎信息并退出循环,如果不相等则
#结束本次循环
if username == user_pass[0] and passwd == user_pass[1]:
print('welcome user %s login !' %username)
flag = True
break
else:
continue
#若flag为真,则⽤户名和密码输⼊正确跳出整个循环体,反之,若⽤户输⼊密码错误的次数为3,则给出提⽰信息:⽤户已经被锁定#并将username追加到⿊名单中
if flag is True:
break
else:
if count == 3:
print('User Have Been try 3 times,Have Been Locked')
lock_file = open('black_mingdan','a')
#lock_file.write('Have Been Locked User:%s\n' %username)
lock_file.write('%s\n' %username)
lock_file.close()
break
程序执⾏结果如下:
[root] python3 denglu.py
python怎么读取py文件please input your name:zhangsan
please input your password:123
welcome user zhangsan login !
[root]# python3 denglu.py
please input your name:zhangsan
please input your password:098
please input your password:678
please input your password:543
User Have Been try 3 times,Have Been Locked
[root]# python3 denglu.py
please input your name:zhangsan
User zhangsan Have Been Locked.It is not allow to login,please contact Administrator. [root]# cat black_mingdan
zhangsan
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论