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 相关文章推荐
gearman的安装启动及python API使用实例
Jul 08 Python
Python生成验证码实例
Aug 21 Python
python数据清洗系列之字符串处理详解
Feb 12 Python
如何在python中使用selenium的示例
Dec 26 Python
python3如何将docx转换成pdf文件
Mar 23 Python
Python虚拟环境的原理及使用详解
Jul 02 Python
Pytorch之Variable的用法
Dec 31 Python
哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程
May 07 Python
Python 创建TCP服务器的方法
Jul 28 Python
一文带你掌握Pyecharts地理数据可视化的方法
Feb 06 Python
python必学知识之文件操作(建议收藏)
May 30 Python
python3 字符串str和bytes相互转换
Mar 23 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
将兴奋、喜悦和坎加斯带到戴安娜:亚马逊公主
2020/03/03 欧美动漫
php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
2010/12/19 PHP
2个比较经典的PHP加密解密函数分享
2014/07/01 PHP
10款实用的PHP开源工具
2015/10/23 PHP
PHP封装类似thinkphp连贯操作数据库Db类与简单应用示例
2019/05/08 PHP
用js实现控制内容的向上向下滚动效果
2007/06/26 Javascript
微信+angularJS的SPA应用中用router进行页面跳转,jssdk校验失败问题解决
2016/09/09 Javascript
JavaScript实现的简单加密解密操作示例
2018/06/01 Javascript
Nodejs Express 通过log4js写日志到Logstash(ELK)
2018/08/30 NodeJs
微信小程序 swiper 组件遇到的问题及解决方法
2019/05/26 Javascript
实现一个 Vue 吸顶锚点组件方法
2019/07/10 Javascript
微信小程序制作扭蛋机代码实例
2019/09/24 Javascript
基于python的汉字转GBK码实现代码
2012/02/19 Python
跟老齐学Python之dict()的操作方法
2014/09/24 Python
详解Python中break语句的用法
2015/05/14 Python
Python 自动刷博客浏览量实例代码
2017/06/14 Python
Python正确重载运算符的方法示例详解
2017/08/27 Python
在CentOS6上安装Python2.7的解决方法
2018/01/09 Python
Python格式化输出%s和%d
2018/05/07 Python
利用Django提供的ModelForm增删改数据的方法
2019/01/06 Python
Python3.8中使用f-strings调试
2019/05/22 Python
Python中turtle库的使用实例
2019/09/09 Python
Python字符串、列表、元组、字典、集合的补充实例详解
2019/12/20 Python
区分python中的进程与线程
2020/08/13 Python
HTML5中微数据概述及在搜索引擎中的使用举例
2013/02/07 HTML / CSS
HTML5拖放效果的实现代码
2016/11/17 HTML / CSS
韩国三星旗下的一家超市连锁店:Home Plus
2016/07/30 全球购物
怎样比较两个类型为String的字符串
2016/08/17 面试题
网友共享的几个面试题关于Java和Unix等方面的
2016/09/08 面试题
本科生职业生涯规划书范文
2014/01/21 职场文书
《大自然的语言》教学反思
2014/04/08 职场文书
服务之星事迹材料
2014/05/03 职场文书
2015年信息技术教研组工作总结
2015/07/22 职场文书
使用Html+Css实现简易导航栏功能(导航栏遇到鼠标切换背景颜色)
2021/04/07 HTML / CSS
Python使用psutil库对系统数据进行采集监控的方法
2021/08/23 Python
Linux在两个服务器直接传文件的操作方法
2022/08/05 Servers