要求:
输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定
本程和作业要求稍有不同,当用户输入三次相同的用户名且无法登陆时锁定该用户名。
下面也会附上符合题目要求的代码,但是存在一个Bug,就是输入三次无法正确登陆只会锁定最后一次输入的用户名。
程序一:
#!usr/bin/evn python #-*-coding utf-8 -*- __author__ = 'Galen' import getpass as gp#引入getpass模块 import pickle#引入pickle模块,pickle提供了一个简单的持久化功能。可以将对象以文件的形式存放在磁盘上。 import os #验证是否存在保存被锁定用户的文件,如果没有则创建一个空的文件,并写入一个空的列表 if not os.path.exists("F:/AAAA资料/Python程序/Python3.5-零基础-高级-完整项目剖析/第1周/lock_user.txt"): lock_user_file = open("lock_user.txt","wb") lock_user = [] pickle.dump(lock_user,lock_user_file) lock_user_file.close() #定义事先设定的username和password常亮 _username ="Galen" _password = "Galen" inpu_user = {}#创建一个空的字典,用于判断一个用户名输入了几遍 count = 0#用于记录输入的次数 while count<3: print("第{}次输入".format(count+1)) username = input("username:") if username not in inpu_user: inpu_user[username]=0 #读取文件 lock_user_file = open("lock_user.txt","rb") lock_user = pickle.load(lock_user_file) lock_user_file.close() #验证输入的用户是否已经被锁定 if username not in lock_user: password = gp.getpass("password:") if username == _username and password == _password: print("Welocme {} come back".format(username)) count +=1 inpu_user[username] +=1 break else: print("you are illegal user") count +=1 inpu_user[username] +=1 else: #如果输入的用户被锁定,可以通过验证解除锁定 print("You have been locked,please contact with administrators") # if input("Are you want to contact administrators :") =="y": if input("Please input your ide_code : ") =="Galen": #输入输入验证码正确则,删除被锁用户名 lock_user_file = open("lock_user.txt","rb") lock_user = pickle.load(lock_user_file) lock_user.remove(username) lock_user_file = open("lock_user.txt","wb") pickle.dump(lock_user,lock_user_file) lock_user_file.close() else: print("You have lost the last chance") break #如果连续输入三次未登陆则锁定用户 if inpu_user[username]==3: lock_user_file = open("lock_user.txt","rb") lock_user = pickle.load(lock_user_file) lock_user.append(username) lock_user_file = open("lock_user.txt","wb") pickle.dump(lock_user,lock_user_file) lock_user_file.close()
程序二:这里将用户名和密码以字典的形式保存在硬盘的文本中
#!usr/bin/evn python #-*-coding utf-8 -*- __author__ = 'Galen' import getpass as gp#引入getpass模块 import pickle import os #验证是否存在保存被锁定用户的文件,如果没有则创建一个空的文件,并写入一个空的列表 if not os.path.exists("F:/AAAA资料/Python程序/Python3.5-零基础-高级-完整项目剖析/第1周/lock_user.txt"): lock_user_file = open("lock_user.txt","wb") lock_user = [] pickle.dump(lock_user,lock_user_file) lock_user_file.close() #创建一个文件保存用户和密码 if not os.path.exists("F:/AAAA资料/Python程序/Python3.5-零基础-高级-完整项目剖析/第1周/user_info.txt"): user_info_file = open("user_info.txt","wb")#打开文件,并赋予写的权限 user_info = {} user_info["Galen"]="Galen" pickle.dump(user_info,user_info_file) user_info_file.close()#关闭文件 user_info_file = open("user_info.txt","rb") user_info=pickle.load(user_info_file) user_info_file.close() #用于记录输入的次数 count = 0 while count<3: print("第{}次输入".format(count+1)) username = input("username:") #读取文件 lock_user_file = open("lock_user.txt","rb") lock_user = pickle.load(lock_user_file) lock_user_file.close() #验证输入的用户是否已经被锁定 if username not in lock_user: password = input("password:") if username in user_info and password == user_info[username]: print("Welocme {} come back".format(username)) count +=1 break else: print("you are illegal user") count +=1 else: #如果输入的用户被锁定,可以通过验证解除锁定 print("You have been locked,please contact with administrators") # if input("Are you want to contact administrators :") =="y": if input("Please input your ide_code : ") =="Galen": #输入输入验证码正确则,删除被锁用户名 lock_user_file = open("lock_user.txt","rb") lock_user = pickle.load(lock_user_file) lock_user.remove(username) lock_user_file = open("lock_user.txt","wb") pickle.dump(lock_user,lock_user_file) lock_user_file.close() else: print("You have lost the last chance") break #如果输入三次未登陆则锁定最后一次输入的用户 if count == 3 : lock_user_file = open("lock_user.txt","rb") lock_user = pickle.load(lock_user_file) lock_user.append(username) lock_user_file = open("lock_user.txt","wb") pickle.dump(lock_user,lock_user_file) lock_user_file.close()