python基础--用户注册与登录再进阶

使用面向对象的方式实现用户的注册与登录

import time
class user:
    def __init__(self,name):
        self.name = name
    @staticmethod
    def login():
        flag = False
        with open("./user_table",mode='r',encoding='utf-8') as file:
                while True:
                    for line in file:
                        use, paw = line.strip().split("|")
                        user_new = input("输入用户名:")
                        if user_new.upper() == "Q":
                            break
                        else:
                            password_new = input("输入密码:")
                            if use == user_new and password_new == paw:
                                flag = True
                                break
                            else:
                                flag = False
                        break
                    if not flag:
                        print("登账号或密码错误")
                    else:
                        print("登录成功")
                        break

    @staticmethod
    def register():
        while True:
            user = input("输入用户名:")
            if user.upper() == "Q":
                break
            elif len(user) == 0:
                print("用户名不能为空")
            else:
                password = input("请输入密码:")
                if len(password) == 0:
                    print("输入的密码不能为空")
                else:
                    line = "{}|{}".format(user,password)
                    with open("./user_table",mode='a',encoding='utf-8')  as file:
                        file.write("{}\n".format(line))
                        file.flush()
                        print("注册成功!")
    @staticmethod
    def show_index():
        print("查看自身信息")
        user_new = input("输入自身用户名:")
        with open("./user_table",mode="r",encoding="utf-8") as file:
            for line in file:
                if user_new ==  line.strip().split("|")[0]:
                    print(line)

class manager(user):
    @staticmethod
    def show_user():
        print("已经注册的用户如下表:")
        with open("./user_table",mode='r',encoding='utf-8') as file:
            for line in file:
                print(line)
    def del_user(self):
        pass
def index():
    print("请选择你要进行功能:")
    print("'1':注册")
    print("'2':登录")
    while True:
        choice = input(">>>")
        if choice == '1' or choice == '2':
            print("输入正确")
            if choice == '1':
                user.register()
                print("是否要进行登录?")
                time.sleep(1)
                print("输入‘q’继续,否则退出")
                new_choice = input("输入>>>")
                if new_choice.upper() == "Q":
                    user.login()
                    print("是否要查询自身信息?")
                    time.sleep(1)
                    print("输入'q'进行查看")
                    choice_new = input("请输入:")
                    if choice_new == "q":
                        user.show_index()
                    print("是否要查看所有已注册人员的信息?")
                    time.sleep(1)
                    password = input("请输入管理员密码:")
                    if password == 'root':
                        manager.show_user()
                    else:
                        print("输入的密码错误")
                else:
                    pass
            else:
                user.login()
                print("是否要查询自身信息?")
                time.sleep(1)
                print("输入'q'进行查看")
                choice_new = input("请输入:")
                if choice_new == "q":
                    user.show_index()
                print("是否要查看所有已注册人员的信息?")
                time.sleep(1)
                password = input("请输入管理员密码:")
                if password == 'root':
                    manager.show_user()
                else:
                    print("输入的密码错误")
            break
        else:
            print("输入不合法")
if __name__ == '__main__':
    index()

你可能感兴趣的:(python基础,案例篇,python,开发语言,前端)