title: python小练习之二
tags: 新建,模板,小书匠
grammar_cjkRuby: true
---
python小练习之二
需求:实现用户登录,用户名和密码保存到文件里,连续输入三次密码错误,则退出
在需求上,略拓展了那么一丢丢:实现用户注册,用户名不存在则引导用户注册,用户注册时检测是否有用户名重复的情况
尚未实现的:检测用户是否存在的时候,没有实现用户名的精确匹配,比如,用户名aa和用户名aaa,如果aaa用户名注册过了,会认为aa用户也注册过了,这块需要修改
# coding:utf-8
class User(object):
def __init__(self):
self.userchoice = 0
self.userlist = list()
self.username = str()
self.password = str()
# 用户名是否被使用了
self.nameused = 0
# 用户的用户名+密码是否能匹配
self.user_correct = 0
# 用户是否存在
self.user_exist = 0
# 用户登录尝试次数
self.login_try_count = 0
# 用户登录允许最大尝试次数为3次
self.login_limit_count = 3
def GenUserList(self):
self.userlist = list()
with open("userlist", "a+") as fd_userlist:
for line in fd_userlist:
if line.rstrip("\n"):
self.userlist.append(line.rstrip("\n"))
def WriteUserList(self, username):
username = self.username
with open("userlist", "a+") as fd_userlist:
fd_userlist.write(self.username)
fd_userlist.write("\n")
def UsernameCheck(self, username):
self.GenUserList()
self.nameused = 0
username = self.username
if self.username in self.userlist:
print '%s 用户名已经使用过了,请重新选择用户名' %(self.username)
self.nameused = 1
elif self.UserExist(self.username) == 1:
self.nameused = 0
return self.nameused
def UserExist(self, username):
with open("userprofile", "a+") as fd:
for line in fd:
if line.find(username) == 0:
self.user_exist = 1
return self.user_exist
else:
self.user_exist = 0
return self.user_exist
def UserRegister(self):
input_username = raw_input("请输入用户名:")
self.username = input_username.strip()
if self.UsernameCheck(self.username) == 0:
input_password = raw_input('请输入密码:').strip()
self.password = input_password
with open('userprofile', 'a+') as fd_userprofile:
# fd_userprofile.write('username:' + self.username + '|')
# fd_userprofile.write('password:' + self.password)
fd_userprofile.write(self.username + '|')
fd_userprofile.write(self.password)
fd_userprofile.write("\n")
self.WriteUserList(self.username)
else:
self.UserRegister()
def UserCorrect(self, username, password):
userProfile_dict_list = list()
with open("userprofile", "a+") as fd:
for line in fd:
u, temp_p = line.split("|")
p = temp_p.strip()
userProfile_dict_list.append({'username': u, 'password':
p})
length = len(userProfile_dict_list)
for i in xrange(length):
if username == userProfile_dict_list[i]['username']:
if password == userProfile_dict_list[i]['password']:
self.user_correct = 1
return self.user_correct
else:
self.user_correct = 0
return self.user_correct
# return self.user_correct
def UserLogin(self):
userProfile_dict_list = list()
input_username = raw_input("登录用户名:").strip()
input_password = raw_input("登录密码:").strip()
self.user_correct = self.UserCorrect(input_username, input_password)
self.user_exist = self.UserExist(input_username)
if self.user_correct == 1:
print "欢迎登录:", input_username
elif self.user_exist == 1:
print "密码错误"
self.login_try_count += 1
print "%s 还有 %d 次尝试机会" %(input_username,\
self.login_limit_count - self.login_try_count)
if self.login_try_count < 3:
self.UserLogin()
else:
print "%s 已经尝试3次登录失败" %(input_username)
self.UserExit()
elif self.user_exist == 0:
print "%s 用户不存在" %(input_username)
print "请去注册"
self.UserRegister()
def UserExit(self):
print "Bye Bye"
exit(0)
def ProcessUserChoice(self):
if self.userchoice == 1:
self.UserRegister()
elif self.userchoice == 2:
self.UserLogin()
elif self.userchoice == 3:
self.UserExit()
else:
self.userchoice = int(raw_input("请输入正确的选择,1或者2或者3:"))
self.ProcessUserChoice()
def InitInterface(self):
failedCount = 0
login_status = dict()
u_profile_dict_list = list()
while True:
try:
self.userchoice = int(raw_input("----------------\n"
"1:注册\n"
"2:登录\n"
"3:退出\n"
"----------------\n"
"请选择:").strip())
self.ProcessUserChoice()
except Exception as e:
print e
self.InitInterface()
def Main():
user = User()
user.InitInterface()
if __name__ == "__main__":
Main()