• day11作业


    #一:今日作业:
    #1、编写文件copy工具
    copy_path = input("请输入复制的文件路径:").strip()
    paste_path = input("请输入粘贴的文件路径:").strip()
    with open(r'{}'.format(copy_path),mode='rt',encoding='utf-8') as f1,
            open(r'{}'.format(paste_path),mode='wt',encoding='utf-8') as f2:
        res = f1.read()
        f2.write(res)
    # 2、编写登录程序,账号密码来自于文件
    user_name = input("请输入账号:").strip()
    pwd = input("请输入密码:").strip()
    with open("user.txt",mode='rt',encoding='utf8') as f:
        for line in f:
            username,userpwd = line.strip().split(":")
            if user_name == username and userpwd == pwd:
                print("登陆成功")
                break
        else:
            print("账号密码错误")
    # 3、编写注册程序,账号密码来存入文件
    user_name = input("请输入账号:").strip()
    user_pwd = input("请输入密码:").strip()
    with open("user.txt",mode='at',encoding='utf-8') as f:
        f.write('{}:{}
    '.format(user_name,user_pwd))
    
    # 二:周末综合作业:
    # 2.1:编写用户登录接口
    # 1、输入账号密码完成验证,验证通过后输出"登录成功"
    # 2、可以登录不同的用户
    # 3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
    
    user_pwd = {"egon":"root123", "alex":"13579", "yu":"root"}
    user_info = {"egon":0, "alex":0, "yu":0}
    user_pwd = {}
    user_info = {}
    with open("user.txt", mode='rt', encoding='utf8') as f:
        for line in f:
            username, userpwd = line.strip().split(":")
            user_pwd.setdefault(username, userpwd)
            user_info.setdefault(username, 0)
    
    while True:
        username = input("请输入账号:").strip()
        pwd = input("请输入密码:").strip()
    
        with open('locked.txt', 'r', encoding='utf-8') as f1:
            all = f1.read()
            a = all.split()
            flag = a[0]
            name = a[1]
        if username not in user_pwd:
            print("用户不存在")
        elif username == name and flag == 'lock':
            print("你的账号已被锁定")
            break
        elif (user_pwd.get(username) != pwd):
            user_info[username] += 1
            print("密码错误")
            if user_info.get(username) >= 3:
                user_info[username] = "lock"
                print("你的账号已被锁定")
                with open("locked.txt",mode='wt',encoding='utf-8') as f2:
                    f2.write("lock "+username)
                break
        else:
            print("登陆成功")
            break
    
    # 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':
            user_pwd = {}
            user_info = {}
            with open("user.txt", mode='rt', encoding='utf8') as f:
                for line in f:
                    username, userpwd = line.strip().split(":")
                    user_pwd.setdefault(username, userpwd)
                    user_info.setdefault(username, 0)
            while True:
                username = input("请输入账号:").strip()
                pwd = input("请输入密码:").strip()
    
                with open('locked.txt', 'r', encoding='utf-8') as f1:
                    all = f1.read()
                    a = all.split()
                    flag = a[0]
                    name = a[1]
                if username not in user_pwd:
                    print("用户不存在")
                elif username == name and flag == 'lock':
                    print("你的账号已被锁定")
                    break
                elif (user_pwd.get(username) != pwd):
                    user_info[username] += 1
                    print("密码错误")
                    if user_info.get(username) >= 3:
                        user_info[username] = "lock"
                        print("你的账号已被锁定")
                        with open("locked.txt", mode='wt', encoding='utf-8') as f2:
                            f2.write("lock " + username)
                        break
                else:
                    print("登陆成功")
                    break
            break
        elif cmd == '2':
            user_name = input("请输入注册账号:").strip()
            user_pwd = input("请输入密码:").strip()
            with open("user.txt",mode='at',encoding='utf-8') as f:
                f.write('{}:{}
    '.format(user_name,user_pwd))
            print("注册成功")
        else:
            print('输入的命令不存在')
    
    # 思考:上述这个if分支的功能否使用其他更为优美地方式实现
    
  • 相关阅读:
    HTTP与HTTPS的区别
    为什么我们越努力 越不安
    最可怕的是牛人还那么努力
    Canvas、Paint、的简单使用及辅助类(Path、Shader、简介)
    创建style和修改style
    Drawable类及XMLDrawable的使用
    反射机制
    Gesture(手势)浅析
    在外部存储器上写入或读取文件(Environment类、File类的使用)
    存储、读取——Android应用程序内置的文件夹
  • 原文地址:https://www.cnblogs.com/yding/p/12489222.html
Copyright © 2020-2023  润新知