python编写登录接口

要求: 输入用户名密码

      认证成功显示欢迎信息

   输错三次以后锁定

代码如下:

# Author:YK
while(True):
select=input('请问是注册还是登录')
if select == '注册':
register='' #将需要保存的user和password放在register中
username = input('please input your count...')
password = input('please input your password...')
register=register+username+','+password+'\n'
file1 = open('D:\\study\\code\\study_teach\\day1\\save_user.txt', 'a+',encoding='utf-8')
#打开保存用户名和密码的文件
file1.write(register) #写入文件
file1.close()


elif select == '登录':
username = input('please input your count...')
file2 = open('D:\\study\\code\\study_teach\\day1\\lock.txt', 'r', encoding='utf-8')
#打开被锁定用户的文件
count=0
number=len(file2.readlines()) #获取文件行数
file2.seek(0, 0) #将鼠标移动到首个字节
while(count<number):
count=count+1
lines = file2.readline().strip('\n') #获取每一行的用户名字符串
if lines == username: #判断输入的用户名是否和存储的用户名相等
print('Your count {name} locked'.format(name=username))
file2.close()
exit()


file1 = open('D:\\study\\code\\study_teach\\day1\\save_user.txt', 'r',encoding='utf-8')
#打开保存用户名和密码的文件
for i in range(3): #有3次机会输入密码的循环
password = input('please input your password...')
count = 0
number = len(file1.readlines()) #获取文件行数
file1.seek(0,0)
while (count < number):
count=count+1
line=file1.readline().strip('\n').split(',')
if line[0] == username and line[1] == password:
print('Welcom to you submit') #登录成功
exit() #退出
if i == 2: #3次均输入错误
file2 = open('D:\\study\\code\\study_teach\\day1\\lock.txt', 'a+',encoding='utf-8')
file2.write(username+'\n') #将被锁定的用户名写入被锁文件当中
file1.close()
file2.close()
print('Your count {name} locked'.format(name=username))
exit()
else:
print('输入有错误,请重新输入')




猜你喜欢

转载自www.cnblogs.com/k-nyz/p/10227030.html