#!/usr/bin/env python #
-*- coding:utf-8 -*-
import os,sys
num_count = 0
lock_file = 'lockfile.txt'
account_file = 'account.txt'
match_flag =''
def lock_input(name):
f = open(lock_file,'a')
f.write('\n'+name)
f.close()
while num_count < 3:
username = raw_input('请输入用户名 :')
password = raw_input('请输入密码 :')
f = open(lock_file,'r')
for lock_name in f.readlines():
if name == lock_name.strip():
sys.exit("你的用户名被锁定了,程序将退出")
f.close()
fd = open(account_file,'r')
for line in fd.readlines():
user,passwd = line.strip('\n').split()
if user == username and passwd == password:
print '登陆成功'
match_flag = True
else:
print "用户名密码不对"
fd.close()
if match_flag == True:
break
else:
print '请重新输入用户名密码!'
num_count += 1 else:
lock_input(username)
########################################################################################
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os,sys
num_count = 0
lock_file = 'lockfile.txt'
account_file = 'account.txt'
match_flag =''
def lock_input(name):
f = open(lock_file,'a')
f.write('\n'+name)
f.close()
def lock_fun(username):
f = open(lock_file,'r')
for lock_name in f.readlines():
if username == lock_name.strip():
return 'lock'
f.close()
def checklogin(username,password):
fd = open(account_file,'r')
for line in fd.readlines():
user,passwd = line.strip('\n').split()
if user == username and passwd == password:
print '登陆成功'
return 'ok'
else:
print "用户名密码不对"
fd.close()
while num_count < 3:
username = raw_input('请输入用户名 :')
password = raw_input('请输入密码 :')
result = lock_fun(username)
if result == 'lock':
sys.exit("此账户已经被锁定,程序将退出")
match_flag = checklogin(username,password)
if match_flag == 'ok':
break
num_count += 1
else:
lock_input(username)
#########################################################################################
使用类 验证时有问题,再找找
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
num_count = 0
lock_file = 'lockfile.txt'
account_file = 'account.txt'
match_flag =''
class Pername(object):
def __init__(self,username,password):
self.Username = username
self.Password = password
def lockcheck(self):
f = open(lock_file,'r')
for i in f.readlines():
user = i.strip('\n').split()
if self.Username == user:
print user + 'lock文件中的名字'
return 'lock'
f.close()
def login(self):
fd = open(account_file,'r')
for line in fd.readlines():
user,passwd = line.strip('\n').split()
if self.Username == user and self.Password == passwd:
return 'userlogin'
fd.close()
def lock_input(self):
f = open(lock_file,'a')
f.write('\n'+self.Username)
f.close()
print "账户已经锁定" if __name__ == '__main__':
while num_count < 3:
username = raw_input('请输入用户名 :')
password = raw_input('请输入密码 :')
example = Pername(username,password)
resultcheck = example.lockcheck()
if resultcheck == 'lock':
sys.exit('此账户已经被锁定,程序将退出')
loginresult = example.login()
if loginresult == 'userlogin':
print "登陆成功"
break
num_count += 1
else:
example.lock_input()