3.17左右 函数注册/登录/用户认证

# 编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def file(filename,old, new):
    import os
    with open(r'{}'.format(filename),mode='rb') as f1, \
            open(r'健康.mp4', mode='wb') as write_f2:
        for line in f1:
            res = line.replace(old, new)
            f1.write(res)
        os.remove(file)
        os.rename('健康.mp4', file)

  编写tail工具

import time
def tail():
    with open("aaa.txt","rb") as f:
        f.seek(0,2)
        while True:
            line = f.readline()
            if len(line) == 0:
                time.sleep(0.3)
            else:
                print(line.decode('utf-8'), end='')

  # 3、编写登录功能

def register():
    user_info={}
    with open('register.txt','at',encoding='utf-8')as f:
        for line in f:
            name,psw=line.strip().split(':')
            user_info[name]=psw
    whlie True:
        username=input('请输入您的用户名').strip()
        if username not in user_info:
            print('用户不存在请重新输入')
            continue    
        userpsd=input('请输入密码').strip()
        if userpsd==user_info.get(username):
            print('登录成功')
            break
        else:
            print('登录失败!')

  4、编写注册

def login():
    with open('ttt.txt','a')as f1,\
        open('into.txt','r')as f2:
        inp_name=input('请输入用户名:').strip()
        inp_word=input('请输入密码:').strip()
        for line in f2:
            name,word=line.strip().split(':')
            if inp_name==name:
                return('该用户名已被注册')
        else:
            f1.write('{name}:{psw}\n'.format(name=inp_name,psw=inp_word))
            return ('注册成功')

  5、编写用户认证功能

def login():
    inp_name = input("用户名:").strip()
    inp_psd = input("密码:").strip()
    with open(r'db.txt', 'rt', encoding='utf-8') as f:
        for line in f:
            name, pwd = line.strip().split(':')
            if inp_name == name and inp_psd == pwd:
                print("登录成功")
                return True
        else:
            print("用户名密码错误")
            return False
def check_user(user_check):
    if user_check:
        print("有趣的事情")
    else:
        print("请先登录")
def main():
    user_check = False
    msg = """
    1、登录
    2、有趣的事情
    """
    tag = True
    dic = {
        '1': True,
        '2': False
    }
    while tag:
        print(msg)
        num = input("请输入编号:").strip()
        if not num.isdigit() and num not in dic:
            print("必须输入指定编号")
        else:
            if dic[num]:
                user_check = login()
            else:
                check_user(user_check)
if __name__ == '__main__':
    main()

  

  

猜你喜欢

转载自www.cnblogs.com/Tornadoes-Destroy-Parking-Lots/p/12513147.html