
正文
pyhton学习,day1作业,用户名密码登录模块
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
要求,通过用户名密码登录,登录错误3次,锁定用户名
# coding=utf-8
# Author: RyAn Bi import os, sys #调用系统自己的库 accounts_file = 'E:\\homework\\user.txt' # 存放用户名密码的位置
lock_file = 'E:\\homework\\lock.txt' #存放锁定用户名的位置
print('accounts_file:', accounts_file)
'''
temp1 = open(accounts_file,'a') #a为加入
temp1.write('bjb,123'+'\n')
temp1.close() #写入用户名密码的功能,在登录的时候不用
'''
user = open(accounts_file, 'r') #r为只读
# print('user:',user)
account_list = user.readlines() #逐行按分隔符阅读,readline是逐字节阅读
# print('account_list:',account_list)
user.close() #读取用户名和密码到内存中 user1 = open(lock_file, "r")
lock_list = user1.readlines()
# print(lock_list)
user1.close() #读取被锁定的用户名到内存中
loginSucess = False #默认将loginSucess置为假
while True:
username = input('username:').strip() #.strip是去除空格和\n
lockid = 0 #锁定用户标识值为零,不锁定
if len(username) != 0: #假如用户名不为空
for i in lock_list:
# i = i.split(' ') #i的分割符为空格
# print(i)
if username == i.strip():
print('your username %s is locked!' % username)
lockid = 1
break # 判断该帐号是否被锁定
for i in account_list:
# print(i)
if lockid == 1: #被锁定了禁止执行
break
else:
i = i.split(',')
# print(i)
if username == i[0]:
for x in range(3):
password = input('password:').strip()
if password == i[1].strip(): # 去掉空格和换行符
loginSucess = True
# print('you are right')
break #验证3次密码,成功了置 loginsuccess 为真
else:
print('%s,you input wrong password 3 times,your %s is locked!' % (username, username))
l = open(lock_file, 'w') #w只允许写入,以写入方式打开locklist
if l != "":
l = open(lock_file, 'a') #假如是locklist中有内容,用添加方式打开locklist,避免空白行
l.write(username + '\n') #将用户名写入
l.close()
lockid = 1
# view = open(lock_file)
# print(view.read())
if loginSucess is True:
print('welcome to system')
break
elif lockid == 0:
print('you input a wrong username,pls input again')
else:
# print('%s,you input wrong password 3 times,your %s is locked!' % (username, username))
break
else: # 用户名输入为空的时候
continue






