Week1-作业:用户登陆程序

#!/usr/bin/en python
# Author:lijun

'''

  输入用户名密码
  认证成功后显示欢迎信息
  输错三次后锁定

'''
import  sys,os
#import  getpass

dir = os.getcwd() passfile = dir+os.sep + "passwd.txt" blacklistfile = dir + os.sep + "blacklist.txt" #读取用户名密码文件: f = open(passfile) user_passwd = {} for line in f: l = line.strip().split(",") user_passwd[l[0]] = l[1] f.close() #读取黑名单 b = open(blacklistfile) blacklist=[] for line in b: blacklist.append(line.strip()) b.close() error_count=0 while error_count<3: error_count = error_count + 1 username = input("Login username:") password = input('passwd:') if username in blacklist: print("您的用户名已经被锁定,请联系管理员。") break if username in user_passwd.keys(): if user_passwd[username] == password: print("Login successfull. Welcome!") break else: print("您输入的账户不存在,请重新输入。") if error_count == 3: if username in user_passwd.keys(): blacklist=open(blacklistfile,"a+") blacklist.writelines(" "+username) blacklist.close() print("您已累计输错三次,您的账号[%s]已经被锁定,请联系管理员." %username) else: print("您输入的账号[%s]不存在,已经累计三次输入失败,程序将退出。." %username)
原文地址:https://www.cnblogs.com/pythonlee/p/9531510.html