练习实现用户增删改查

class User_massage:
    def __init__(self, name, age, hobby):
        self.name = name
        self.age = age
        self.hobby = hobby

    def get_massage(self):
        print('展示信息:')
        for i in self.__dict__:
            print(f'{i}:{getattr(self, i)}')


    def change_massage(self):
        user_input = input('请输入你要改的属性>>:')
        change_value = input('要把原来的值改成>>:')
        if hasattr(self, user_input):
            setattr(self, user_input, change_value)
        else:
            print('没有此属性')

    def del_massage(self):
        user_input = input('请输入你要删除的属性>>:')
        if hasattr(self, user_input):
            delattr(self, user_input)
        else:
            print('没有此属性')


while True:
    name = input('请输入用户名字>>>:')
    age = input('请输入用户年龄>>>:')
    hobby = input('请输入用户爱好>>>:')
    obj = User_massage(name, age, hobby)
    while True:
        user_choice = input('''
    1.get_massage
    2.change_massage
    3.del_massage
    请输入要执行的功能(q退出)>>>:''')
        if user_choice in dir(obj):
            getattr(obj, user_choice)()
        elif user_choice == 'q':
            break
        else:
            print('没有此功能')
class User(object):
    def __init__(self, name, age, hobby):
        self.name = name
        self.age = age
        self.hobby = hobby


class Student(User):
    pass


obj = Student('张三', 18, '读书')
while True:
    print("""
    1.查
    2.增
    3.改
    4.删
    """)
    num = input('请输入需要使用的功能')
    name = input('请输入要用的属性')
    if num == '2':
        if not hasattr(obj, name):
            vlues = input('请输入值')
            setattr(obj, name, vlues)
            print(obj.__dict__)
            continue
        else:
            print('该属性存在哟')
    if not hasattr(obj, name):
        print('没有这个名字哦')
        continue

    if num == '1':
        print(getattr(obj, name))
        continue

    if num == '3':
        vlues = input('请输入值')
        setattr(obj, name, vlues)
        print(obj.__dict__)
        continue
    if num == '4':
        delattr(obj, name)
        print(obj.__dict__)
        continue
    # print('编号输入不正确')
posted @ 2022-11-07 23:25  小福福  阅读(18)  评论(0编辑  收藏  举报