Ⅹ:作业

1.编写文件copy工具

src_file = input(">>>请输入要拷贝的文件路径:").strip()
copy_file = input(">>>请输入拷贝文件的存放位置:").strip()
with open(r'{}'.format(src_file), 'rt', encoding='utf-8') as f,\
        open(r'{}\copy.txt'.format(copy_file), 'wt', encoding='utf-8') as copy_f:
    copy = f.read()
    copy_f.write(copy)

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

print("登录")
inp_user = input(">>>your name:").strip()
inp_pwd = input(">>>your password:").strip()
with open(r'user.txt', 'rt', encoding='utf-8') as f:
    for line in f:
        user, pwd = line.strip().split(':')
        if user == inp_user and pwd == inp_pwd:
            print('login success')
            break
    else:
        print('用户名或密码错误')

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

print("注册")
inp_user = input(">>>your name:").strip()
inp_pwd = input(">>>your password:").strip()
with open(r'user.txt', 'at', encoding='utf-8') as f:
    f.write('{user}:{pwd}\n'.format(user=inp_user, pwd=inp_pwd))

周末综合作业:

2.1:编写用户登录接口

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

inp_name=input('请输入你的名字:').strip()
with open (r'b.txt',mode='r',encoding='utf-8')as f:
    for line in f:
        name = line.strip()
        if name == inp_name:
            print('账号被锁定')
            break
    else:
        count = 0
        while count < 3:
            # inp_name = input('请输入你的名字:').strip()
            inp_pwd = input('请输入您的密码:').strip()
            with open('c.txt', mode='rt', encoding='utf-8')as f:
                for j in f:
                    u, p = j.strip().split(':')
                    if inp_name == u and inp_pwd == p:
                        print('登陆成功')
                        count = 4
                        break
                else:
                    print('账号或密码错误')
                    count += 1
        else:
            if count == 3:
                print('输错三次,锁定')
                with open('b.txt', mode='a', encoding='utf-8')as f1:
                    f1.write('{}\n'.format(inp_name))

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分支的功能否使用其他更为优美地方式实现
posted @ 2020-03-13 18:46  迷路的玖儿  阅读(251)  评论(0编辑  收藏  举报