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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Python函数式编程指南(三):迭代器详解

3. 迭代器 3.1. 迭代器(Iterator)概述 迭代器是访问集合内元素的一种方式。迭代器对象从集合的第一个元素开始访问,直到所有的元素都被访问一遍后结束。 迭代器不能回退,只能往...

python实现维吉尼亚加密法

python实现维吉尼亚加密法

本文实例为大家分享了python实现维吉尼亚加密法的具体代码,供大家参考,具体内容如下 Vigenere加密/解密时,把英文字母映射为0-25的数字再进行运算,并按n个字母为一组进行变换...

Python判断中文字符串是否相等的实例

Python判断中文字符串是否相等的实例

Python判断两个相等的中文字符串为false,将两个待比较的字符串都把unicode编码设为‘utf-8'也不能解决问题,具体原因如下: 1.首先查看待比较两个字符串的编码格式 ,使...

wxpython中自定义事件的实现与使用方法分析

本文实例讲述了wxpython中自定义事件的实现与使用方法。分享给大家供大家参考,具体如下: 创建自定义事件的步骤: ① 定义事件类,该事件类必须继承自wx.PyCommandEvent...

Python字符串的常见操作实例小结

本文实例讲述了Python字符串的常见操作。分享给大家供大家参考,具体如下: 如果我们想要查看以下功能:help(mystr .find) 1.find 例: mystr="hell...