day11 本日作业+周末作业

一、今日作业

1、编写文件copy工具

with open("a.txt",mode="r",encoding="utf-8") as f1 ,open("b.txt",mode="w",encoding="utf-8") as f2:
    f2.write(f1.read())

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

user_inp = input("请输入用户名:")
pwd_inp = input("请输入密码:")
with open("a.txt",mode="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
    else:
        print("账号或秘密错误")

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

username = input("请输入账号:").strip()
password = input("请输入密码:")
with open("b.txt","a",encoding="utf-8") as f :
    f.write("
{}:{}".format(username,password))

二、周末综合作业:

1、编写用户登录接口

1、输入账号密码完成验证,验证通过后输出"登录成功"

2、可以登录不同的用户

3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)

import os
count = 0
tag = True
list1 = []
while tag:
    username = input("请输入用户名:")
    if os.path.exists("locked{}".format(username)):
        print("用户被锁定")
        count=0
        break
    password = input("请输入密码:")
    with open("a.txt",mode="r",encoding="utf-8") as f :
        for line in f :
            user,pwd = line.strip().split(":")
            if username == user and password == pwd:
                print("登录成功")
                tag = False
                break
        else:
            if list1.count(username) ==2:
                with open("locked{}".format(username),"w") as f :
                    print("错误3次该用户被锁定")
            else:
                list1.append(username)
                print(list1.count(username))
                print("登录失败")

2、编写程序实现用户注册后,可以登录

import os
list2=[]
list1=["1","2","3"]
while True:
    print('''
    1.注册
    2.登录
    3.退出
    ''')
    cmd = input("cmd>")
    tag=True
    if cmd == list1[0]:
        username = input("请输入你要注册的账号:")
        password = input("请输入你要注册的密码:")
        with open("a.txt","a",encoding="utf-8") as f :
            f.write(f"{username}:{password}
")
    elif cmd == list1[1]:
        while tag:
            use_inp = input("请输入你的用户名:")
            if os.path.exists(f"locked/{use_inp}"):
                print("账号被锁定")
                tag = False
                break
            else:
                pwd_inp = input("请输入你的密码:")
                with open("a.txt","r",encoding="utf-8") as f :
                    for line in f :
                        username,password=line.strip().split(":")
                        if use_inp==username and password==pwd_inp:
                            print("登录成功")
                            tag = False
                            break
                    else:
                        if list2.count(use_inp) == 2:
                            with open(f"locked/{use_inp}","w",encoding="utf-8") :
                                pass
                            print("账号被锁定")
                            break
                        else:
                            print("输入错误")
                            list2.append(use_inp)
                            print(list2.count(use_inp))

    elif cmd == list1[2]:
        break
    else:
        print("非法输入")
原文地址:https://www.cnblogs.com/hz2lxt/p/12487649.html