Python文件操作模拟用户登陆代码实例


Posted in Python onJune 09, 2020

题目要求

1、输入用户名和密码后回车

2、密码输入错误,给出提示,并选择是否重新输入

3、密码输入错误三次后,用户被锁定,无法继续登陆

构思

1、用户输入账号和密码后,需要判断账号是否存在

2、判断账号是否被禁用(错误次数大于三次)

3、判断账号密码是否正确

4、不同的错误给出不同的提示

5、每输入错一次,文档中的错误次数需要更新

6、如果三次以内用户登陆成功,密码原来的错误次数被重置

题目完成步骤

1、文档的编写

考虑到数据的存储问题,决定将账号、密码、错误次数进行分行存储,三行为一组用户信息

Python文件操作模拟用户登陆代码实例

2、代码编写

go = True
while go:
  # 用来判断账号是否存在
  no_existence_flag = True
  # 用来判断是否输入正确
  no_flag = True
  # 用来判断是否已经被封
  disable_flag = True
  # 用来判断次数是否已经超过限制
  account = input("account:")
  password = input("password:")
  # 判断账号是否存在(自己写入已存在用户的账号密码)
  file = open("C:/Users/Lenovo/Desktop/user.txt","r")
  # 用于拼接文本内容
  file_data = ""
  while True:
    line = file.readline()
    if not line:
      break
    file_data += line
    line_content = line.strip()
    # 判断是否存在账号
    if account == line_content:
      no_existence_flag = False
      true_password = file.readline()
      file_data += true_password
      true_password_content = true_password.strip()
      disable_flag_line = file.readline()
      disable_flag_num = int(disable_flag_line.strip())
      # 判断账号是否被禁用
      if disable_flag_num != 3:
        print("It is not disable!",disable_flag_num)
        disable_flag = False
        # 判断密码是否正确
        if password == true_password_content:
          no_flag = False
          print("Welcome in this system,{account}!".format(account = account))
          go = False
          disable_flag_line = disable_flag_line.replace(str(disable_flag_num),str(0))
          file_data += disable_flag_line
        else:
          disable_flag_line = disable_flag_line.replace(str(disable_flag_num),str(disable_flag_num+1))
          file_data += disable_flag_line
      else:
        file_data += file.readline()
    else:
      file_data += file.readline()
      file_data += file.readline()
  file.close()
  # 账号不存在的报错
  if no_existence_flag:
    print("This account is not existence!")
    print("Do you want to try it again......")
    flag = input("Please input you think:")
    if flag == "N":
      go = False
    continue
  # 账号被禁用的报错
  if disable_flag:
    print("You account is disable,please go home by youself!")
    print("Do you want to try it again......")
    flag = input("Please input you think:")
    if flag == "N":
      go = False
    continue
  # 账号密码错误的报错
  if no_flag:
    file = open("C:/Users/Lenovo/Desktop/user.txt","w")
    print(file_data)
    file.write(file_data)
    file.close()
    print("Your password is not right,please try it again!")
    print("Do you want to try it again......")
    flag = input("Please input you think:")
    if flag == "N":
      go = False
  # 重置输入次数
  else:
    file = open("C:/Users/Lenovo/Desktop/user.txt","w")
    print(file_data)
    file.write(file_data)
    file.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
用Python和MD5实现网站挂马检测程序
Mar 13 Python
Python是编译运行的验证方法
Jan 30 Python
Python命令行参数解析模块optparse使用实例
Apr 13 Python
Python基于pygame实现图片代替鼠标移动效果
Nov 11 Python
Python实现爬取马云的微博功能示例
Feb 16 Python
Python获取时间范围内日期列表和周列表的函数
Aug 05 Python
python内置模块collections知识点总结
Dec 19 Python
Python selenium抓取虎牙短视频代码实例
Mar 02 Python
pyMySQL SQL语句传参问题,单个参数或多个参数说明
Jun 06 Python
numpy的Fancy Indexing和array比较详解
Jun 11 Python
在Python中实现字典反转案例
Dec 05 Python
Python中递归以及递归遍历目录详解
Oct 24 Python
pyCharm 实现关闭代码检查
Jun 09 #Python
在pycharm中关掉ipython console/PyDev操作
Jun 09 #Python
python 元组的使用方法
Jun 09 #Python
解决pycharm中的run和debug失效无法点击运行
Jun 09 #Python
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
Jun 09 #Python
解决pycharm debug时界面下方不出现step等按钮及变量值的问题
Jun 09 #Python
PyCharm MySQL可视化Database配置过程图解
Jun 09 #Python
You might like
PHP制作图型计数器的例子
2006/10/09 PHP
用php过滤危险html代码的函数
2008/07/22 PHP
php写的简易聊天室代码
2011/06/04 PHP
用 Composer构建自己的 PHP 框架之设计 MVC
2014/10/30 PHP
tp5框架基于ajax实现异步删除图片的方法示例
2020/02/10 PHP
firefox 和 ie 事件处理的细节,研究,再研究 书写同时兼容ie和ff的事件处理代码
2007/04/12 Javascript
jQuery $.data()方法使用注意细节
2012/12/31 Javascript
jquery实现marquee效果(文字或者图片的水平垂直滚动)
2013/01/07 Javascript
nullJavascript中创建对象的五种方法实例
2013/05/07 Javascript
JS使用getComputedStyle()方法获取CSS属性值
2014/04/23 Javascript
jQuery中append()方法用法实例
2015/01/08 Javascript
JavaScript中getUTCMinutes()方法的使用详解
2015/06/10 Javascript
Jquery $when done then的用法详解
2016/05/20 Javascript
JS获取屏幕高度的简单实现代码
2016/05/24 Javascript
BootStrap表单验证实例代码
2017/01/13 Javascript
一篇文章搞定JavaScript类型转换(面试常见)
2017/01/21 Javascript
Angular指令封装jQuery日期时间插件datetimepicker实现双向绑定示例
2017/01/22 Javascript
jquery hover 不停闪动问题的解决方法(亦为stop()的使用)
2017/02/10 Javascript
react实现换肤功能的示例代码
2018/08/14 Javascript
vue+element UI实现树形表格带复选框的示例代码
2019/04/16 Javascript
如何使用CSS3+JQuery实现悬浮墙式菜单
2019/06/18 jQuery
python模块restful使用方法实例
2013/12/10 Python
Python使用openpyxl读写excel文件的方法
2017/06/30 Python
Python实现DDos攻击实例详解
2019/02/02 Python
使用Python计算玩彩票赢钱概率
2019/06/26 Python
Django 创建后台,配置sqlite3教程
2019/11/18 Python
利用pandas向一个csv文件追加写入数据的实现示例
2020/04/23 Python
Python实现播放和录制声音的功能
2020/08/12 Python
巴西香水和化妆品购物网站:The Beauty Box
2019/09/03 全球购物
会计学应届毕业生推荐信
2013/11/04 职场文书
中秋节超市促销方案
2014/01/30 职场文书
开展批评与自我批评心得体会
2014/10/17 职场文书
财政局个人年终总结
2015/03/03 职场文书
2016年员工年度考核评语
2015/12/02 职场文书
学习心理学心得体会
2016/01/22 职场文书
PYTHON基于Pyecharts绘制常见的直角坐标系图表
2022/04/28 Python