作业 —— day11

周五作业:

1.编写文件copy工具

src_file = input('原文件路径:').strip()
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.编写登录程序,账号密码来自于文件

print("========欢迎来到登录界面========")
inp_name = input("请输入用户名:").strip()
inp_pwd = input("请输入密码:").strip()
with open(r'userinfo.txt', 'rt', encoding='UTF-8') as f:
    for line in f:
        user, pwd = line.strip().split(':')
        if user == inp_name and pwd == inp_pwd:
            print('恭喜您,登录成功!')
            break
    else:
        print('用户名或密码错误!')
print("==============================")
userinfo.txt的内容:
	qwe:123
	asd:456
	zxc:789

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

print("========欢迎来到注册界面========")
inp_user = input("请输入用户名:").strip()
inp_pwd = input("请输入密码:").strip()
with open(r'userinfo1.txt', 'at', encoding='utf-8') as f:
    f.write('{}:{}\n'.format(inp_user,inp_pwd))
print("============注册成功============")

周末作业:

编写用户登录接口

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

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

    if cmd == '0':
        break
    elif cmd == '1':
        # 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
        tag = True
        count = 0
        locked = False
        while tag:
            inp_usr = input("请输入用户名:").strip()
            with open(r'locked.txt', 'rt', encoding='utf-8') as f0:
                for line in f0:
                    locked = line.strip()
                    if inp_usr == locked:
                        print("该用户已被锁定")
                        locked = True
                        break
            if locked:
                locked = False
                continue
            else:
                inp_pwd = input("请输入密码:").strip()
                with open(r"userinfo.txt", mode="rt", encoding="UTF-8") as f1:
                    for line in f1:
                        username, password = line.strip().split(":")
                        if inp_usr == username and inp_pwd == password:
                            print("登录成功")
                            tag = False
                            break
                    else:
                        count += 1
                        print('您已输错{}次,还剩{}次'.format(count,3-count))
                        if count == 3:
                            print('帐号{}已输错3次,帐号锁定'.format(inp_usr))
                            with open(r"locked.txt", mode="wt", encoding="UTF-8") as f2:
                                f2.write('{}\n'.format(inp_usr))
                                print('输错3次,退出程序')

    elif cmd == '2':
        # 注册功能代码
        username = input("请输入账号:").strip()
        password = input("请输入密码:")
        with open("userinfo.txt", "at", encoding="UTF-8") as f:
            f.write("{}:{}\n".format(username, password))
            print('注册成功!')
    else:
        print('输入的命令不存在')

Final

import os
list2=[]
tag = True
while True:
    print('''
    1.注册
    2.登录
    3.退出
    ''')
    tag = True
    cmd = input("请输入指令>")
    list1 = ["1","2","3"]
    if cmd == list1[0]:
        username = input("请输入要注册的用户名:")
        password = input("请输入要注册的密码:")
        with open("a.txt","a",encoding="utf-8") as f :
            # f.write(f"{username}:{password}\n")
            f.write("{}:{}\n".format(username,password))
    elif cmd == list1[1]:
        while tag:
            user_inp = input("请输入你的用户名:")
            if os.path.exists(f"locked/{user_inp}"):
                print("账号被锁定")
                tag =False
                continue
            else:
                pwd_inp = input("请输入你的密码:")
                with open("a.txt","r",encoding="utf-8") as f:
                    for line in f:
                        username,password = line.strip().split(":")
                        if username == user_inp and password == pwd_inp:
                            print("登录成功")
                            break
                    if list2.count(user_inp) == 2:
                        with open(f"locked/{user_inp}","w",encoding="utf-8"):
                            print("错误太多被锁定")
                    else:
                        print("输入错误")
                        list2.append(user_inp)
                        print(list2.count(user_inp))
    elif cmd == list1[2]:
        print("谢谢")
        break
    else:
        print("非法输入")
posted @ 2020-03-13 16:48  轻描丨淡写  阅读(280)  评论(0编辑  收藏  举报