登录程序

# 登录程序:
# 1、账号、密码不能为空
# 2、要校验账号是否存在
# 3、最多输入3次。
# 4、账号不区分大小写
# 5、账号、密码长度要大于等于6且小于等于12
# 6、每次登录的时候密码错误,就在账号后面加一个失败次数
# 思路
# 1、读取文件,读取用户名和密码、次数,保存到字典里
# 2、最多错误三次
# 3、输入账号、密码
# 4、校验不为空
# 5、校验长度[6-12]
# 6、校验账号是否存在
# 7、校验密码,不区分大小写,对则登录成功
# 8、密码错误有提示且文件里失败数加一


user_info = {}
f = open('user.txt', 'r+', encoding='utf-8')
f.seek(0)
for line in f:
    if line.strip() != '':
        line = line.strip()
        username, pwd, error_count = line.split(',')
        user_info[username] = {'password': pwd, 'error_count': int(error_count)}

print(user_info)

for i in range(3):
    username = input('username:').strip()
    pwd = input("password:").strip()
    if username == '' or pwd == '':
        print("账号、密码、确认密码不能为空!")
    elif 12 < len(username) or len(username) < 6 or 12 < len(pwd) or len(pwd) < 6 :
        print("账号、密码 长度不对")
    elif username not in user_info:
        print("用户名不存在")
    elif pwd != user_info[username]['password']:
        print("密码错误")
        user_info[username]['error_count'] += 1
    elif user_info[username]['error_count'] > 3:
        print("密码错误次数太多")
    else:
        print("登录成功")
        break
else:
    print("次数用完了,稍后在试吧")

print(user_info)

f.seek(0)
for username, user_dict in user_info.items():
    pwd = user_dict.get('password')
    error_count = user_dict.get('error_count')
    user_str = '\n%s,%s,%s' % (username,pwd,error_count)
    f.write(user_str)

f.close()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值