用户登录注册

 
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2018/5/24 11:08
# @File : atm_demo.py
 
 

with open('user_pass.txt', 'r', encoding='utf-8') as user_lis, open('user_status.txt', 'r', encoding='utf-8') as user_stat:
    use_stat_li = user_stat.read().split(',')
    use_lis_dic = {}
    for i in user_lis:
        use_lis_dic.setdefault(i.split()[0], i.split()[1])


def user_regi(arg):
    if arg not in use_lis_dic:
        use_regi = arg
        use_pa_regi = input('输入密码')
        use_pa_regi2 = input('再次输入密码')
        if use_pa_regi == use_pa_regi2:
            user_new_write = open('user_pass.txt', 'a', encoding='utf-8')
            user_new_write.write('\n'+use_regi + ' ' + use_pa_regi)
            user_new_write.close()
            print('用户', arg, '注册成功,请登录')

        else:
            print('密码输入不一致')
    else:
        print('当前用户名已经注册,请重新输入')


def login(name):
    count = 0
    for i in range(5):
        # name = input('请输入用户名>>')
        if name not in use_stat_li:  # 是否在黑名单中

            # continue  # 在黑名单中直接进入下一次循环,重新输入用户名和密码
            if name in use_lis_dic:
                passwd = input('请输入密码>>')
                if passwd == use_lis_dic.get(name):
                    print('欢迎老用户', name, '回来')
                    break
                else:  # 密码错误
                    if count == 2:
                        name_bak_write = open('user_status.txt', 'a', encoding='utf-8')
                        name_bak_write.write(name + ',')
                        name_bak_write.close()  # 3次密码错误,就把用户名写进黑名单
                        print('用户名已锁定,请联系管理员!!!')
                        break
                    print('密码错误!!!')
                    count += 1  # 密码错一次,count就+1
        elif name  in use_lis_dic:
            print('用户名已被锁定,请联系管理员!!!')
            # print('当前用户不存在系统中,y注册,n登录')
            # xu = input('输入选择')
            # if xu == 'y':
            #     user_regi(name)
            break
            # continue
        else:
            if i == 4:  # 用户名错5次,直接退出
                print('尝试次数过多,再见!!!')
                continue


def init_sys():
    name = input('请输入用户名>>')
    if name in use_lis_dic:
        login(name)
    elif name not in use_lis_dic:
        print('当前用户不存在系统中,y注册,n登录')
        xu = input('输入选择')
        if xu == 'y':
            user_regi(name)
        elif xu == 'n':
            login(name)
        else:
            print('输入正确指令')


if __name__ == '__main__':
    init_sys()

 
 
所需文件:
------------------------
user_pass.txt
 
alex 123
xiaoming 456
xiaoli 1234
alek 123456
albk 123
albb 123
albkk albkk
fmgao 123
fmgao1 123
dog 123
 
 
--------------------------
user_status.txt
 
alex,xiaoli,fmgao,dog,
 
 
 

猜你喜欢

转载自www.cnblogs.com/fmgao-technology/p/9089768.html