代码改变世界

编写登录接口

2018-07-15 21:44  小于漫谈  阅读(197)  评论(0编辑  收藏  举报
import os,getpass,sys
i = 0
while i < 3:
name = input("input name:")
lock_file = open('account_lock.txt', 'r+')
lock_list = lock_file.readlines()
for lock_line in lock_list:
if name == lock_line.strip(): #此处加strip去掉后边的空格,因为读取时候有空格
sys.exit('username %s is locked' %name)
user_file = open('account.txt', 'r')
user_list = user_file.readlines()
for user_line in user_list:
(user,password) = user_line.strip().split()
if name == user:
j = 0
while j < 3:
passwd = input("please input your password:")
if passwd == password:
print('login sucess %s' % name)
sys.exit(0)
else:
if j != 2:
print("your %s password is wrong,please input again,you have %d choice:"%(name,2-j))
j+=1
else:
lock_file.write(name + '\n') #写入时候有后边应该带空格
sys.exit('user %s reaches the maximum number of logins and is locked out'% name)
else:
if i != 2:
print('user %s does not exist,please input again,you have %d choice'%(name,2-i))
i+=1
else:
sys.exit('user does not exist,exit')
lock_file.close()
user_file.close()