zoukankan      html  css  js  c++  java
  • Python

    Python的第十三天

    练习:用户登录认证程序

       1、要求用户输入账号密码进行登录

       2、用户账号信息保存在文件内

       3、用户账号密码输入三次后锁定用户,下次在登录,检测到是这个被锁定的用户,则依然不允许其登录,提示已被锁

      

    #1、确定在文件里存储的账号信息的结构
    #2、把账号数据读到内存,为了方便调用,可以改成list或dict
    accounts = {
    # "alex":["alex","33h44","1"]
    }
    f = open("account.db","r")
    for line in f:
    line = line.strip().split(",")
    accounts[line[0]] = line
    print(accounts)
    #3、用loop,要求用户输入账号信息,去判断

    while True:
    user = input("username:").strip()
    if user not in accounts: #用户未注册
    print("用户未注册。。。")
    continue
    elif accounts[user][2] == "1":#用户账号被锁定
    print("用户账号被锁定")
    continue
    count = 0
    while count < 3:
    passwd = input("password:").strip()
    # 判断password是否和字典里的密码一致
    if passwd == accounts[user][1]:
    print(f"welcome {user}..登陆成功。。")
    exit("bye...")
    else:
    print("Password wrong....")
    count += 1
    if count == 3:
    print(f"共输错了{count}次密码,{user}的账号已被锁定。。")
    #1、先改内存中dict账号信息的用户状态
    #2、把dict里的数据转成原account.db格式,并存回文件
    accounts[user][2] = "1"
    f2 = open("account.db","w")
    for user.val in accounts.items():
    line = "".join(user.val) + " "#把列表再转成字符
    f2.write(line)
    f2.close()
    exit("bye..")
  • 相关阅读:
    2020年12月15日Java学习日记
    2020年12月12日Java学习日记
    2020年12月10日Java学习日记
    2020年12月8日Java学习日记
    2020年12月4日Java学习日记
    2020年12月1日Java学习日记
    2020年11月30日Java学习日记
    2020年11月27日Java学习日记
    2020年11月26日Java学习日记
    B. Navigation System【CF 1320】
  • 原文地址:https://www.cnblogs.com/sxy2021/p/14374320.html
Copyright © 2011-2022 走看看