python--用户登录

来源:互联网 发布:著名网络暴力事件 编辑:程序博客网 时间:2024/05/17 02:57
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:xuejpimport sys#输入用户名密码username = input('username:')password = input("password:")#读取文件并转化为字典with open('./User','r') as f:    result = dict(line.strip().split(':') for line in f if line)#设置计数器count = 0#用户是否存在def if_user_exit():    count = 0    for key in result:        if username == key:            count+=1    if count == 0:        print('The user is not exist')        sys.exit()#检查用户是否被锁def if_user_lock():    with open('./lock_user', 'r') as f:  # 检车该用户是否被lock        for line in f.readlines():            if username == line.strip():                print('The user is locked')                sys.exit()if_user_exit()if_user_lock()while count < 3:    if password == result.get(username): #用户名密码正确跳到登录界面        info = '''welcome user {_username} login'''.format(_username=username)        print(info)        break    else: #用户名密码不正确,告知登录失败,计数器加一        if_user_exit() #判断第二次输入的用户是否存在        count += 1        if count == 3:  # 首先判断是否错误三次            print('sorry,the user %s is locked!' % (username))  # 第三次登录失败告知用户被锁            with open('./lock_user', 'a') as f_lock:  # 将用户名写到文件中                f_lock.write(username + '\n')        else:            print("login filed:three filed will be lock,only have {_count} changes".format(_count=3-count))            username = input('username:')            password = input("password:")

流程图
这里写图片描述

原创粉丝点击