3/15

# 2.1:编写用户登录接口
#1、输入账号密码完成验证,验证通过后输出"登录成功"
#2、可以登录不同的用户
#3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)

user_name=input('请输入用户名: ')
password=input('请输入密码: ')
tag = True
count=0
with open('pwd.txt',mode='rt',encoding='utf-8') as file1,\
        open('lock.txt',mode='rt',encoding='utf-8') as file2:
    for i in file2:
        lock_name,lock_pwd=i.strip().split(':')
        if user_name==lock_name :
            print('该账号已被锁定!')
            tag = False
            break
        else:
            continue
    while tag:
        for j in file1:
            name,pwd=j.strip().split(':')
            if user_name==name and password==pwd:
                print('登录成功')
                tag=False
        else:
            print('登录失败!')
            count+=1
            if count==3:
                print('失败次数过多,该账号被锁定!')
                with open('lock.txt',mode='at',encoding='utf-8')as file3:
                    res=('{}:{}\n'.format(user_name,password))
                    file3.write(res)
                tag=False
                break
            else:
                user_name = input('请再次输入用户名: ')
                password = input('请再次输入密码: ')
# 2.2:编写程序实现用户注册后,可以登录,
# 提示:
while True:
    msg = """
    0 退出
    1 登录
    2 注册
    """
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
        print('必须输入命令编号的数字,傻叉')
        continue

    if cmd == '0':
        break
    elif cmd == '1':
        # 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
        n = 0
        tag = True
        while tag:
            user_name = input('请输入用户名: ')
            password = input('请输入密码: ')
            with open('pwd.txt', mode='rt', encoding='utf-8')as file1:
                for i in file1:
                    name, pwd = i.strip().split(':')
                    if user_name == name and password == pwd:
                        print('登录成功!')
                        tag = False
                        break
                else:
                    print('账号或密码错误!')
                    n += 1
                    if n == 3:
                        tag = False
        break
    elif cmd == '2':
        # 注册功能代码
        user_name = input('请输入注册id:')
        password = input('请输入注册密码: ')
        with open('pwd.txt', mode='at', encoding='utf-8')as file2:
            res = ('{}:{}\n'.format(user_name, password))
            file2.write(res)
            print('注册成功!')
        break
    else:
        print('输入的命令不存在')

猜你喜欢

转载自www.cnblogs.com/bailongcaptain/p/12499382.html
315