Python实现账号密码输错三次即锁定功能简单示例

yipeiwu_com5年前Python基础

本文实例讲述了Python实现账号密码输错三次即锁定功能。分享给大家供大家参考,具体如下:

初学Python—1

#实现账号输错三次即锁定
user = "hubery"
passwd = "123"
confirm = 0
lock=0
fileOpen = open("username.txt","a+")
fileOpen.seek(0)
for i in range(3):
 username = input("username:")
 passsword = input("password:")
 for line in fileOpen.readlines():
  if username == line.strip():
   print("账户已经锁定!")
   lock=1
   break
  else:
   continue
 fileOpen.seek(0)
 if user == username and lock ==0:
  if passwd == passsword:
   print("欢迎,欢迎!")
   confirm = 1
   break
  else:
   print("账号户或者密码错误!")
   continue
 elif lock==1:
  continue
 else:
  print("1账号或者密码错误!")
  continue
fileOpen.close()
if confirm == 0 and lock==0:
 fileWrite=open("username.txt","a")
 fileWrite.write(username+"\n")
 fileWrite.close()

基本功能可以实现;

锁定的账号为第三次输错的用户名(待完善)

以下为完善版本,如有错误,请告知

import os
user = "hubery"
passwd = "123"
count = 0
lock = 0
fileOpen = open("username.txt", "a+")
fileOpen.seek(0)
while 1:
 for i in range(5):
  username = input("username:")
  passsword = input("password:")
  for line in fileOpen.readlines():
   if username == line.strip():
    print("账户已经锁定!")
    lock = 1
    break
   else:
    continue
  fileOpen.seek(0)
  if user == username:
   if lock == 1:
    continue
   elif passsword == passwd:
    print("欢迎,欢迎!")
    os._exit(0)
   elif count < 2:
    print("账号或者密码错误!")
    count += 1
    continue
   else:
    fileOpen.write(username + "\n")
    fileOpen.flush()
    print("密码输入错误超过三次,账户已经锁定!")
    fileOpen.seek(0)
    continue
  else:
   print("账号密码错误!")
   continue
 check=input("还想验证其他账户?(yes-继续,no-退出)")
 if "no"==check.lower():
  os._exit(0)
 else:
  continue
fileOpen.close()

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

使用pyecharts生成Echarts网页的实例

pyecharts是一个封装百度开源图表库echarts的包,使用pyecharts可以生成独立的网页,也可以在flask、django中集成使用。 示例如下: from pyech...

PyQt5显示GIF图片的方法

PyQt5显示GIF图片的方法

使用QMoive方法实现 导入库文件 from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import QMovi...

Python给你的头像加上圣诞帽

Python给你的头像加上圣诞帽

引言 随着圣诞的到来,大家纷纷@官方微信给自己的头像加上一顶圣诞帽。当然这种事情用很多P图软件都可以做到。但是作为一个学习图像处理的技术人,还是觉得我们有必要写一个程序来做这件事情。而且...

python字典的遍历3种方法详解

python字典的遍历3种方法详解

遍历字典: keys() 、values() 、items()   1. xxx.keys() : 返回字典的所有的key 返回一个序列,序列中保存有字典的所有的键   效果图:   ...

Python多进程库multiprocessing中进程池Pool类的使用详解

Python多进程库multiprocessing中进程池Pool类的使用详解

问题起因 最近要将一个文本分割成好几个topic,每个topic设计一个regressor,各regressor是相互独立的,最后汇总所有topic的regressor得到总得预测结果。...