zoukankan      html  css  js  c++  java
  • day11

    # 1) 接收用户输入的账号与密码
    username = input('请输入账号: ').strip()
    password = input('请输入密码: ').strip()
    print(f'用户输入的账号与密码是: {username} {password}')

    # 2) 从文件中读取用户的数据进行校验
    with open('user_info.txt', 'r', encoding='utf-8') as f:
    res = f.read()
    print(res)
    # user_list = res.split(':')
    # print(user_list)
    # user_info.txt中没有换行符的情况下
    user, pwd = res.split(':')
    print(f'文件中存放的账号与密码是: {user} {pwd}')
    if username == user and password == pwd:
    print('登录成功!')
    else:
    print('登录失败')
    '''

    # 2、可以登录不同的用户
    '''
    tag = True
    while tag:
    # 1) 接收用户输入的账号
    username = input('请输入账号: ').strip()

    # 2) 从user_info2.txt文件中读取用户的数据进行校验
    with open('user_info2.txt', 'r', encoding='utf-8') as f:
    for user_pwd in f:
    # user_info2.txt中有换行符的情况下
    # 先将换行符去掉,然后再根据:号进行切割
    user, pwd = user_pwd.strip().split(':')
    # print(f'文件中存放的账号与密码是: {user} {pwd}')

    # 3) 先校验用户名
    if username == user: # 若账号存在则成立,否则不成立
    print(username)

    # 4) 若用户名存在,继续输入密码
    while True:
    password = input('请输入密码: ').strip()
    if password == pwd:
    # 登录成功后,结束所有循环
    print('登录成功!')
    tag = False
    break

    else:
    print('登录失败!')
    '''


    # 3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
    # user_info3.txt

    # 答案
    '''
    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步骤
    '''

    # 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':
    # 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
    pass
    elif cmd == '2':
    # 注册功能代码
    pass
    else:
    print('输入的命令不存在')

    # 思考:上述这个if分支的功能否使用其他更为优美地方式实现
    '''

    # 答案
    '''
    dic = {
    '0': '退出',
    '1': '登录',
    '2': '注册'
    }

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

    # 判断当前用户如果不在dic字典中,执行
    if cmd not in dic:
    print('输入的命令不存在')
    continue

    print(dic.get(cmd))
    '''
  • 相关阅读:
    PowerDesigner快捷键
    Android 的开源电话/通讯/IM聊天项目全集
    Android ContentProvider完整案例
    Android中观察者模式的升入理解
    Android中Socket大文件断点上传
    Storm概念学习系列之Tuple元组(数据载体)
    开始使用storm
    Storm概念学习系列之storm的功能和三大应用
    Storm概念学习系列之storm的特性
    Storm概念学习系列之storm核心组件
  • 原文地址:https://www.cnblogs.com/2722127842qq-123/p/12787483.html
Copyright © 2011-2022 走看看