zoukankan      html  css  js  c++  java
  • 3-13 作业

     1.编写文件copy工具

    1 src_file = input(">>>请输入要拷贝的文件路径:").strip()
    2 copy_file = input(">>>请输入拷贝文件的存放位置:").strip()
    3 with open(r'{}'.format(src_file), 'rt', encoding='utf-8') as f,
    4         open(r'{}copy.txt'.format(copy_file), 'wt', encoding='utf-8') as copy_f:
    5     copy = f.read()
    6     copy_f.write(copy)
    
    
    while True:
        src_file = input('源文件路径>>: ').strip()
        # 校验源文件路径是否存在
        import os
    
        if not os.path.exists(src_file):
            print('源文件不存在')
            continue
    
        dst_file = input('备份路径>>: ').strip()
    
        with open(r'{}'.format(src_file), mode='rt', encoding='utf-8') as f1, 
                open(r'{}'.format(dst_file), mode='wt', encoding='utf-8') as f2:
            res = f1.read()
            f2.write(res)
    优质版
    
    

    2、编写登录程序,账号密码来自于文件
     1 print("登录")
     2 inp_user = input(">>>your name:").strip()
     3 inp_pwd = input(">>>your password:").strip()
     4 with open(r'user.txt', 'rt', encoding='utf-8') as f:
     5     for line in f:
     6         user, pwd = line.strip().split(':')
     7         if user == inp_user and pwd == inp_pwd:
     8             print('login success')
     9             break
    10     else:
    11         print('用户名或密码错误')
    
    
    # user_info.txt文件中内容为
    '''
    tank:123
    '''
    
    
    '''
    username = input('请输入用户名: ')
    password = input('请输入密码: ')
    with open('user_info.txt', 'r', encoding='utf-8') as f:
        user_info = f.read()
        user, pwd = user_info.split(':')
    
    if username == user and password == pwd:
        print('登录成功!')
    else:
        print('登录失败!')
    '''
    View Code
    
    

    3、编写注册程序,账号密码来存入文件
    1 print("注册")
    2 inp_user = input(">>>your name:").strip()
    3 inp_pwd = input(">>>your password:").strip()
    4 with open(r'user.txt', 'at', encoding='utf-8') as f:
    5     f.write('{user}:{pwd}
    '.format(user=inp_user, pwd=inp_pwd))

    周末作业

    # #二:周末综合作业:
    # # 2.1:编写用户登录接口
    # #1、输入账号密码完成验证,验证通过后输出"登录成功"
    # user_info2.txt文件中
    '''  ---- user_info2.txt文件中数据 ----
    tank:123
    
    '''
    # 答案
    '''
    while True:
        username = input('请输入用户名: ').strip()
        password = input('请输入密码: ').strip()
        with open('user_info2.txt', 'r', encoding='utf-8') as f:
            user, pwd = f.read().strip().split(':')
            if username == user and password == pwd:
                print('登录成功!')
                break
            else:
                print('用户名或密码错误!')
    '''
    
    # #2、可以登录不同的用户
    '''  ---- user_info3.txt文件中数据 ----
    tank:123
    egon:321
    
    '''
    # 答案
    '''
    tag = True
    while tag:
        username = input('请输入用户名: ').strip()
        with open('user_info3.txt', 'r', encoding='utf-8') as f:
            for line in f:
                user, pwd = line.strip().split(':')
                # print(user, pwd)
                if username == user:
                    password = input('请输入密码: ').strip()
                    if password == pwd:
                        print('登录成功!')
                        tag = False
                        break
                    else:
                        print('密码错误!')
    '''
    
    # #3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
    '''  ---- user_info4.txt文件中数据 ----
    tank:123:0
    egon:321:0
    
    '''
    # 答案
    '''
    tag = True
    while tag:
        username = input('请输入用户名: ').strip()
        with open('user_info4.txt', 'r', encoding='utf-8') as f, open('user_info5.txt', 'w', encoding='utf-8') as w:
            for line in f:
                user, pwd, locked = line.strip().split(':')
                # print(user, pwd, type(locked))
                locked = int(locked)
    
                if locked == 3:
                    print('当前用户已被锁定')
    
                if username == user:
                    while locked < 3:
                        password = input('请输入密码: ').strip()
                        if password == pwd:
                            print('登录成功!')
                            tag = False
                            break
                        else:
                            print('密码错误!')
                            locked += 1
    
                w.write(f'{user}:{pwd}:{locked}
    ')
    
        import os
        os.remove('user_info4.txt')
        # 将user_info5.txt修改为user_info4.txt
        os.rename('user_info5.txt', 'user_info4.txt')
    '''
    
    # # 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
    
        if cmd not in dic:
            print('输入有误')
            continue
    
        print(dic.get(cmd))
    '''
    周末作业详解
  • 相关阅读:
    教学计划-物理必修二
    小白学习Python之路---开发环境的搭建
    解决pycharm连接MySQL 1366报错的问题
    Leetcode 118 杨辉三角
    Leecode 70 爬楼梯
    RabbitMQ
    Leetcode 38 报数
    Leecode 69 x的平方根
    select 实现server I/O多路复用通信
    Leetcode 67 二进制求和
  • 原文地址:https://www.cnblogs.com/2722127842qq-123/p/12486876.html
Copyright © 2011-2022 走看看