python(14)- 简单练习:登录账户失败三次,账户自动锁定

题目需求:

  1.输入用户名密码

  2.认证成功后显示欢迎信息

  3.输错三次后锁定

 

#读取注册用户的信息,用户名,密码,输错次数,写入字典中
user={}
with open("DB1",encoding="utf8") as f:
    for line in f:
        username_list=line.strip().split("|")      #username_list--->['egon', '123', '2']
        user[username_list[0]]={"name":username_list[0],
                 "pwd":username_list[1],
                 "times":username_list[2]}
# print(user)  #-->{'egon': {'name': 'egon', 'pwd': '123', 'times': '2'}, 'xuyaping': {'name': 'xuyaping', 'pwd': '123', 'times': '0'}, 'xyy': {'name': 'xyy', 'pwd': '123', 'times': '1'}}

#读取黑名单用户,将黑名单用户加入列表中
with open("black_lockname",encoding="utf8") as f1:
    black_list=[]
    for line in f1:
        black_list.append(line.strip())
# print(black_list)


while True:
    username = input("please input your username:").strip()
    passwd = input("please input your passwd:").strip()
    #用户在黑名单中
    if username in black_list:
        print("该用户为黑名单用户,请滚")
        break

    # 用户为注册用户
    elif username in user:
        user[username]["times"]=int(user[username]["times"])
        if user[username]["times"]<3 and passwd==user[username]["pwd"]:
            print("登录成功")
            user[username]["times"]=0
            #将修改后的信息重新写入DB1中
            with open("DB1","w",encoding="utf8") as f3:
                for i in user:
                    f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "\n")
            break

        else:
            user[username]["times"]+=1
            print("登录错误")
            # 将修改后的信息重新写入DB1中
            with open("DB1", "w", encoding="utf8") as f3:
                for i in user:
                    f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "\n")
            if user[username]["times"]==3:
                black_list.append(username)
                print("账户被锁定")
                # 将修改后的信息重新写入black_lockname中
                with open("black_lockname","w",encoding="utf8") as f4:
                    for j in black_list:
                        f4.write(j+ "\n")
                break

    #用户不是注册用户
    else:
        print("该用户没有注册")
        break

 

xuyaping|123|0
egon|123|1
xuyuanyuan|123|2
DB1

 

alex
black_lockname

 

posted @ 2017-04-07 17:38  许二哈哈哈  阅读(1381)  评论(0编辑  收藏  举报