day09作业

#一:今日作业:
#1、编写文件copy工具

src_file_path=input("请输入原文件路径:>>>").strip()
des_file_path=input("请输入拷贝文件路径:>>>").strip()
with open(r"{}".format(src_file_path),"r",encoding="utf-8") as src_file_read,\
    open(r"{}".format(des_file_path),"w",encoding="utf-8") as des_file_write:
    for line in src_file_read:
        des_file_write.write(line)

 

#2、编写登录程序,账号密码来自于文件

# 登录
inp_name=input("请输入账号:>>>").strip()
inp_pwd=input("请输入密码:>>>").strip()
with open("a.txt","r",encoding="utf-8") as f:
    for line in f:
        user_name,passwd=line.strip("\n").split(":")
        if inp_name == user_name and inp_pwd == passwd:
            print("登录成功。")
            break
    else:
        print("账号或密码错误。")

 

#3、编写注册程序,账号密码来存入文件

flag=False   #判断是否用户已经存在

add_name=input("请输入用户名>>")
add_pass=input("请输入密码>>")

with open("db.txt","r",encoding="utf-8") as f:
    for line in f:
       if add_name in line:
           flag=True
if flag == False:
    with open("db.txt","a",encoding="utf-8") as f:
        f.write("\n{}:{}".format(add_name,add_pass))
        print("用户{}注册成功".format(add_name))
else:
    print("用户{}已存在。".format(add_name))

 

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

//目录结构:

//user_db.txt文件内容

zhangsan:123:0
lisi:123:0
egon:123:0
alex:123:0

//代码:

import os,time
flag=True
count=[]        #收集用户输错次数
lock_flush=False                #检测需要刷新锁定

while flag:
    inp_name=input("请输入账号:>>>").strip()
    inp_pwd=input("请输入密码:>>>").strip()

    #检测是否锁定
    with open("user_db.txt", "r", encoding="utf-8") as f:
        for line in f:
            user_name,passwd,tag=line.strip("\n").split(":")
            # print(type(tag))
            if inp_name == user_name and tag == "1":
                print("用户已被锁定,请5分钟后再试。。")
                time.sleep(300)
                lock_flush=True
    #刷新锁定
    if lock_flush == True:
        with open("user_db.txt", "r", encoding="utf-8") as f, \
                open("user_db.swap", "w", encoding="utf-8") as f_new:
            for line in f:
                if inp_name in line:
                    user_name,passwd,tag = line.strip("\n").split(":")
                    tag = "0"
                    f_new.write("{}:{}:{}\n".format(user_name, passwd, tag))
                else:
                    f_new.write(line)
        os.remove("user_db.txt")
        os.rename("user_db.swap", "user_db.txt")
    # 登录
    with open("user_db.txt","r",encoding="utf-8") as f:
        for line in f:
            user_name,passwd,tag=line.strip("\n").split(":")
            if inp_name == user_name and inp_pwd == passwd:
                print("登录成功。")
                flag=False
                break
        else:
            print("账号或密码错误。")
            count.append(inp_name)
    #刷新文件
    if count.count(inp_name) == 2:
        with open("user_db.txt", "r", encoding="utf-8") as f,\
            open("user_db.swap", "w", encoding="utf-8") as f_new:
            for line in f:
                if inp_name in line:
                    user_name, passwd,tag = line.strip("\n").split(":")
                    tag="1"
                    f_new.write("{}:{}:{}\n".format(user_name,passwd,tag))
                else:
                    f_new.write(line)
        os.remove("user_db.txt")
        os.rename("user_db.swap","user_db.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分支的功能否使用其他更为优美地方式实现

//目录结构:

//user_db.txt文件内容

zhangsan:123
lisi:123
egon:123
alex:123

//代码

flag=True #是否退出
count=0 #计算用户输错次数
name_existed=False # 判断用户是否
while flag:
    msg = """
    0 退出
    1 登录
    2 注册
    """
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if cmd.isdigit():
        if cmd == '0':
            break

        elif cmd == '1':
            # 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
            while flag:
                inp_name = input("请输入账号:>>>").strip()
                inp_pwd = input("请输入密码:>>>").strip()
                if count == 2:
                    print("已经输错三次,退出程序。")
                    flag=False
                    break
                with open("info_db.txt", "r", encoding="utf-8") as f:
                    for line in f:
                        user_name, passwd = line.strip("\n").split(":")
                        if inp_name == user_name and inp_pwd == passwd:
                            print("登录成功。")
                            flag=False
                            break
                    else:
                        print("账号或密码错误。")
                        count+=1

        elif cmd == '2':
            add_name=input("请输入账号:>>>").strip()
            add_pwd=input("请输入密码:>>>").strip()
            # 判断用户是否已经存在
            with open("info_db.txt", "r", encoding="utf-8") as f:
                for line in f:
                    if add_name in line:
                        name_existed=True
            if name_existed == False:
                #注册
                with open("info_db.txt","a",encoding="utf-8") as f:
                    f.write("\n{}:{}".format(add_name,add_pwd))
                    print("用户{}注册成功。".format(add_name))
            else:
                print("用户{}已经存在".format(add_name))
        else:
            print('输入的命令不存在')
    else:
        print('必须输入命令编号的数字,傻叉')

 

posted @ 2020-03-13 17:34  耗油炒白菜  阅读(191)  评论(0编辑  收藏  举报