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()

python小练习之二的更多相关文章

  1. Python小练习(二)

    按照下面的要求实现对列表的操作:       1)产生一个列表,其中有40个元素,每个元素是0到100的一个随机整数       2)如果这个列表中的数据代表着某个班级40人的分数,请计算成绩低于平均 ...

  2. Python小代码_5_二维矩阵转置

    使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...

  3. python小例子(二)

    1.在函数里面修改全局变量的值 2.合并两个字典.删除字典中的值 3.python2和python3 range(1000)的区别 python2返回列表,python3返回迭代器 4.什么样的语言可 ...

  4. python小算法(二)

    有两个序列a,b,大小都为n,序列元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小.(华为面试) def diff(sorted_list ...

  5. python小工具myqr生成动态二维码

    python小工具myqr生成动态二维码 (一)安装 (二)使用 (一)安装 命令: pip install myqr 安装完成后,就可以在命令行中输入 myqr 查看下使用帮助: myqr --he ...

  6. 机器学习算法与Python实践之(二)支持向量机(SVM)初级

    机器学习算法与Python实践之(二)支持向量机(SVM)初级 机器学习算法与Python实践之(二)支持向量机(SVM)初级 zouxy09@qq.com http://blog.csdn.net/ ...

  7. python机器学习实战(二)

    python机器学习实战(二) 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7159775.html 前言 这篇noteboo ...

  8. Python小数据池,代码块

    今日内容一些小的干货 一. id is == 二. 代码块 三. 小数据池 四. 总结 python小数据池,代码块的最详细.深入剖析   一. id is == 二. 代码块 三. 小数据池 四. ...

  9. Python入门基础学习 二

    Python入门基础学习 二 猜数字小游戏进阶版 修改建议: 猜错的时候程序可以给出提示,告诉用户猜测的数字偏大还是偏小: 没运行一次程序只能猜测一次,应该提供多次机会给用户猜测: 每次运行程序,答案 ...

随机推荐

  1. ambari下 hive metastore 启动失败

    由字符集引起的hive 元数据进程启动失败 解决方法新增 这2句话 reload(sys)sys.setdefaultencoding('utf8')

  2. 防F12审查元素扒代码:按下F12关闭当前页面

    有的时候我看别人的网站的某个部分做的比较好我都会通过按F12审查元素来查看别人的代码,那么如果不然别人查看自己网站的代码呢.一段JavaScript代码即可实现上述功能,插入到footer.php或者 ...

  3. 【Unity3D与23种设计模式】策略模式(Strategy)

    GoF中定义: "定义一组算法,并封装每个算法,让它们之间可以彼此交换使用. 策略模式让这些算法在客户端使用它们时能更加独立." 游戏开发过程中 不同的角色会有不同的属性计算方法 ...

  4. 记kkpager分页控件的使用

    kkpager支持异步加载分页: 1.页面添加div标签和引用JS,默认标签为<div id="kkpager"></div> 引用JS和样式 <sc ...

  5. IPFS: Merkle DAG数据结构

    今天带大家来深入探索一下IPFS的核心数据结构Merkle DAG 什么是 Merkle DAG? Merkle DAG是IPFS系统的核心概念之一,当然Merkle DAG并不是IPFS团队发明的, ...

  6. python文件读read()、readline()、readlines()对比

    读取文件的三个方法:read().readline().readlines().均可接受一个变量用以限制每次读取的数据量,但通常不使用.本章目的是分析和总结三种读取方式的使用方法和特点. 一.read ...

  7. java-线程实现方式

    实现方式: 1,继承Thread类 public class ThreadTest extends Thread { @Override public void run() { System.out. ...

  8. 优化的四个方面,缓存,表结构,索引,SQL语句

    一,缓存 数据库属于 IO 密集型的应用程序,其主要职责就是数据的管理及存储工作.而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所 ...

  9. Python中的unittest和logging

    今天使用Python的unittest模块写了些单元测试,现记录下要点: 使用unittest的基本格式如下: import unittest class Test(unittest.TestCase ...

  10. Beta第五天

    听说