zoukankan      html  css  js  c++  java
  • 用户登入注册作业

    #二:周末综合作业:
    # 2.1:编写用户登录接口
    #1、输入账号密码完成验证,验证通过后输出"登录成功"
    #2、可以登录不同的用户
    #3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
    
    tag = True
    dict = {}
    while tag:
        inp_name = input('输入账号:')
        with open(r'b.txt', mode='r', encoding='utf-8') as f1:
            if inp_name in f1:
                print('该账户已被锁定')
                tag = False
                break
        inp_paw = input('输入密码:')
        with open(r'a.txt', mode='rt', encoding='utf-8') as f:
            for line in f:
                name,pwd = line.strip().split(':')
                if name == inp_name and pwd == inp_paw:
                    print('登入成功')
                    tag = False
                    break
                elif name == inp_name and pwd != inp_paw:
                    print('密码错误')
                    if name not in dict:
                        dict[name]=0
                    dict[name] = dict.get(name)+1
                    if dict[name] == 3:
                        with open(r'b.txt', mode='a', encoding='utf-8') as f1:
                            f1.write(name)
                            print('该用户被锁定')
                            tag = False
                    break
            else:
                print('账号不存在')
    
    
    
    
    
    
    
    # 2.2:编写程序实现用户注册后,可以登录,
    dict = {}
    while True:
        msg = """
        0 退出
        1 登录
        2 注册
        """
        print(msg)
        cmd = input('请输入命令编号>>: ').strip()
        if not cmd.isdigit():
            print('必须输入命令编号的数字,傻叉')
            continue
    
        if cmd == '0':
            break
        elif cmd == '1':
            inp_name = input('输入账号:')
            with open(r'b.txt', mode='r', encoding='utf-8') as f1:
                if inp_name in f1:
                    print('该账户已被锁定')
                    break
            inp_paw = input('输入密码:')
            with open(r'a.txt', mode='rt', encoding='utf-8') as f:
                for line in f:
                    name,pwd = line.strip().split(':')
                    if name == inp_name and pwd == inp_paw:
                        print('登入成功')
                        break
                    elif name == inp_name and pwd != inp_paw:
                        print('密码错误')
                        if name not in dict:
                            dict[name]=0
                        dict[name] = dict.get(name)+1
                        if dict[name] == 3:
                            with open(r'b.txt', mode='a', encoding='utf-8') as f1:
                                f1.write(name)
                                print('该用户被锁定')
                        break
                else:
                    print('账号不存在')
    
        elif cmd == '2':
            inp_name = input('输入账号:')
            inp_paw = input('输入密码:')
            with open(r'a.txt', mode='rt',encoding='utf-8') as f:
                for line in f:
                    name, pwd = line.strip().split(':')
                    if name == inp_name:
                        print('账号已存在')
                        break
                else:
                    with open(r'a.txt', mode='at', encoding='utf-8') as f:
                        f.write('{}:{}
    '.format(inp_name,inp_paw))
                        print('注册成功')
        else:
            print('输入的命令不存在')
    
        # 思考:上述这个if分支的功能否使用其他更为优美地方式实现
    #二:周末综合作业:
    # 2.1:编写用户登录接口
    #1、输入账号密码完成验证,验证通过后输出"登录成功"
    #2、可以登录不同的用户
    #3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)

    tag = True
    while tag:
    # 1) 接收用户输入的账号
    username = input('请输入账号: ').strip()

    # 2) 从user_info3.txt文件中读取用户的数据进行校验
    with open('user_info3.txt', 'r', encoding='utf-8') as f,
    open('user_info_copy.txt', 'w', encoding='utf-8') as w: # w模式

    # 将user_info3.txt文件中的每一行数据读取出来,然后赋值给user_pwd
    for user_pwd in f:
    # user_info3.txt中有换行符的情况下
    # 先将换行符去掉,然后再根据:号进行切割
    user, pwd, count = user_pwd.strip().split(':')
    # print(f'文件中存放的账号与密码是: {user} {pwd}')
    count = int(count)
    # 3) 先校验用户名
    if username == user: # 若账号存在则成立,否则不成立

    # print(username)
    # 4) 若用户名存在,初始让用户输入三次机会,继续输入密码
    while count < 3: # 初始 0<3
    password = input('请输入密码: ').strip()
    if password == pwd:
    # 登录成功后,结束所有循环
    print('登录成功!')
    tag = False
    break
    else:
    print('登录失败!')
    count += 1

    # 若当前用户的计数为3,代表已被锁定
    if count == 3: # 当用户计数为3时,打印锁定
    print('当前用户以被锁定')

    # 5) 在此处将,未修改与修改后的用户数据写入user_info_copy.txt文件中
    w.write(f'{user}:{pwd}:{count} ')

    # 6) 删除user_info3.txt文件
    import os
    os.remove('user_info3.txt')

    # 7) 将user_info_copy.txt改名为user_info3.txt文件
    os.rename('user_info_copy.txt', 'user_info3.txt')
    # 作业: 通过文件处理时限 6、7步骤






    dic = {
    '0': '退出',
    '1': '登录',
    '2': '注册',
    }
    tag = True
    while tag:
    msg = """
    0 退出
    1 登录
    2 注册
    """
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
    print('必须输入命令编号的数字,傻叉')
    continue

    if cmd not in dic:
    print('输入有误')
    continue

    print(dic.get(cmd))
    if cmd == '0':
    break

    elif cmd == '1':
    while tag:
    username = input('请输入用户名: ').strip()
    password = input('请输入密码: ').strip()
    with open('a.txt', 'r', encoding='utf-8') as f:
    for line in f:
    user, pwd = line.strip().split(':')
    if username == user and password == pwd:
    print('登录成功!')
    tag = False
    break
    elif username == user and password != pwd:
    print('密码错误')
    break
    else:
    print('无此用户')

    elif cmd == '2':
    while tag :
    inp_name = input('输入账号:')
    with open(r'a.txt', mode='rt',encoding='utf-8') as f,open(r'a.txt', mode='at', encoding='utf-8') as w:
    for line in f:
    name, pwd = line.strip().split(':')
    if name == inp_name:
    print('账号已存在')
    break
    else:
    inp_paw = input('输入密码:')
    w.write('{}:{} '.format(inp_name,inp_paw))
    print('注册成功')
    tag = False
  • 相关阅读:
    Log4j.properties配置详解
    在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean
    Java定时任务:利用java Timer类实现定时执行任务的功能
    java关于Timer schedule执行定时任务 !!!!!!!!!
    java关于Timer schedule执行定时任务
    Cannot call sendRedirect() after the response has been committed错误;
    http status 301/302 & java重定向/转发
    jQuery判断checkbox是否选中的3种方法
    怎样从Mysql官网下载mysql.tar.gz版本的安装包
    (转)Nagios 配置及监控
  • 原文地址:https://www.cnblogs.com/bk134/p/12499470.html
Copyright © 2011-2022 走看看