首页 > 其他分享 >练习实现用户增删改查

练习实现用户增删改查

时间:2022-11-07 23:34:50浏览次数:40  
标签:obj name self 练习 改查 print user 增删 input

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('编号输入不正确')

标签:obj,name,self,练习,改查,print,user,增删,input
From: https://www.cnblogs.com/xiao-fu-zi/p/16867890.html

相关文章

  • 简单的反射增删改查
    #coding:utf-8importosimportsysimportjsonroot_dir=os.path.dirname(os.path.dirname(__file__))sys.path.append(root_dir)db_dir=os.path.join(root_dir,'db')......
  • PYthon基础之面向对象:反射方法实战—增删改查
    classUser_cls_info(object):def__init__(self,name,age,gender,hobby,):self.__name=nameself.__age=ageself.__gander=ge......
  • 常用公用用例设计-增删改查
    常用公用用例设计-增删改查一、保存操作1、保存成功,数据库增加新纪录——OK2、保存失败,数据库没有新增纪录——OK3、非必填项不输入保存——OK4、......
  • marddown练习
    标题#+空格名字一级标题二级标记##+空格+名字二级标题三级标题##+空格三级标题四级标记最多六级字体粗体两边各两个**helloworld斜体两边各一个hellowor......
  • python练习题-数据类型-字符串方法-字符串验证类方法(五)
    #coding=utf-8#1.startswith(prefix[,start[,end]])检查字符串是否是以指定子字符串prefix开头print"abcefg".startswith("abc");#2.endswith检查字符串......
  • ES拼音搜索分词器练习
    #拼音分词器GET_analyze{"text":"如家酒店","analyzer":"pinyin"}#ik分词器GET_analyze{"text":"如家酒店","analyzer":"ik_smart"}DELETEtestPUT/test......
  • 练习-笔记-jcmaxx33
    记录MATLAB数字信号处理练习笔记eg-1(2022-11-04)symsx;f=sin(x)/(x^2+4*x+3);y=diff(f,x,4)定义符号函数求指定阶导数 eg-2(2022-11-06)Fs=1000;%Sampling......
  • 牛客练习赛105(A-D)
    A-切蛋糕的贝贝题解:分成1:1:4:5:1:4份,每次都要沿着两点连线切割,所以n要是16的倍数#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;typedefpair<l......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:二叉树中的最大路径和
    题目:路径被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中至多出现一次。该路径至少包含一个节点,且不一定经过根节......
  • matlab练习程序(Z-N法整定)
    PID调参时可以先利用Z-N法估算一个大概的量,然后再精确调参。Z-N法可以形象的用以下图表表示:其中切线是响应曲线转折点切线,即过曲线斜率最大点的切线。K是系统开环稳态......