Python十天入门之第三天 类和对象

                                    学员管理系统

"""
1.添加学员
    1.1 输入学员姓名、年龄、电话
    1.2 创建一个字典,将输入的姓名、年龄、电话存入字典中
    1.3 将字典添加到大列表中
    存储的格式:[{'name':'张三','age':22,'phone':110},{},{},{},{},{}]
2.查询学员
    2.1 查询所有学员
        2.1.1 遍历大列表,取出小字典
        2.1.2 从字典根据key取出值,并输出
    2.2 根据关键字查询
        2.2.1 输入一个查询关键字
        2.2.2 遍历大列表,取出小字典
        2.2.3 判断字典中的name对应的值是否包含关键字,若包含,输出信息
3.修改学员
    3.1 输出所有学员信息
    3.2 选择要修改的学员索引,输入选择的索引
    3.3 根据索引取出小字典
    3.4 输入修改后的值
    3.5 根据key修改字典中的值
4.删除学员
    4.1 选择索引删除
        4.1.1 输出所有学员信息
        4.1.2 选择要删除的学员索引,输入选择的索引
        4.1.3 根据选择的索引删除学员信息
    4.2 删除所有学员
0.退出程序
"""


# 添加学员
def add_stu():
    """
    添加学员
    :return:None
    """
    while True:
        # 1.输入学员姓名 年龄  电话
        name = input('*    请输入学员姓名,输入q结束添加:')
        if name == 'q':
            break
        age = input('*    请输入学员年龄:')
        phone = input('*    请输入学员电话:')
        # 2.创建一个字典,将输入的姓名、年龄、电话存入字典中
        stu = {'name': name, 'age': age, 'phone': phone}
        # 3.将创建的字典添加到大列表中
        students.append(stu)


# 查询所有学员
def query_all():
    """
    查询所有学员信息
    :return: None
    """
    if len(students) == 0:
        print('*    暂无学员信息,请稍后重试~')
        return

    # 遍历大列表,取出小字典中的数据
    for x in range(0, len(students)):
        # 根据索引取出小字典
        stu = students[x]
        name = stu['name']
        age = stu['age']
        phone = stu['phone']
        print('*    索引:%s   姓名:%s   年龄:%s   电话:%s' % (x, name, age, phone))


# 根据关键字查询学员
def query_of_keyword():
    """
    根据关键字查询学员信息
    :return: None
    """
    # 1.输入查询关键字
    kw = input('*   请输入查询关键字:')

    # 2.遍历大列表,取出小字典
    for x in range(0, len(students)):

        stu = students[x]
        # 3.取出字典中的name
        name = stu['name']
        # 4.判断name中是否包含kw
        if kw in name:
            age = stu['age']
            phone = stu['phone']
            print('*    索引:%s   姓名:%s   年龄:%s   电话:%s' % (x, name, age, phone))


# 查询学员
def query():
    """
    查询学员
    :return: None
    """
    if len(students) == 0:
        print('*        暂无学员信息,请稍后重试~')
        return
    print('*    a.查询所有学员')
    print('*    b.输入关键字查找')
    select = input('*   请选择查询方式:')

    while select != 'a' and select != 'b':
        select = input('*   选项有误,请重选:')

    if select == 'a':
        # 查询所有学员
        query_all()
    else:
        # 根据关键词查找
        query_of_keyword()


# 修改学员
def modify_stu():
    """
    修改学员信息
    :return:
    """
    if len(students) == 0:
        print('*    暂无学员信息,无法执行修改操作!')
        return
    # 3.1 输出所有学员信息
    query_all()
    # 3.2 选择要修改的学员索引,输入选择的索引,判断索引是否在范围
    idx = input('*    请输入要修改的学员索引:')
    idx = int(idx)
    while idx < 0 or idx >= len(students):
        idx = input('*    索引有误,请重选:')
        idx = int(idx)
    # 3.3 根据索引取出小字典
    stu = students[idx]
    # 3.4 输入修改后的值
    new_name = input('*    请输入修改后的姓名(%s):' % stu['name'])
    new_age = input('*    请输入修改后的年龄(%s):' % stu['age'])
    new_phone = input('*    请输入修改后的电话(%s):' % stu['phone'])
    # 3.5 根据key修改字典中的值
    # 字典[key] = 值
    stu['name'] = new_name
    stu['age'] = new_age
    stu['phone'] = new_phone
    print('*    修改成功!')


# 删除学员
def del_stu():
    """
    删除学员信息
    :return: None
    """
    if len(students) == 0:
        print('*    暂无学员信息,无法执行删除操作!    ')
        return
    print('*    a.选择索引删除')
    print('*    b.删除所有学员')
    select = input('*    请选择删除方式:')
    while select != 'a' and select != 'b':
        select = input('*   选项有误,请重选:')

    # 4.1 选择索引删除
    if select == 'a':
        # 4.1.1 输出所有学员信息
        query_all()
        # 4.1.2 选择要删除的学员索引,输入选择的索引
        idx = input('*    选择要删除的学员索引:')
        idx = int(idx)
        while idx < 0 or idx >= len(students):
            idx = input('*    索引有误,请重选:')
            idx = int(idx)
        # 4.1.3 根据选择的索引删除学员信息
        stu = students[idx]

        is_del = input('*    确认要删除(%s)吗?y/n:' % stu['name'])

        if is_del == 'y':

            del students[idx]
            print('*    删除成功!')


    else:
        # 4.2 删除所有学员
        is_del = input('*   确认要删除所有学员信息?y/n:')
        # 确认删除?
        if is_del == 'y':

            students.clear()
            print('*    删除成功!')


students = []

while True:

    print('****     智游学员管理系统V2.0      ****')
    # Ctrl + d 快速复制粘贴
    print('             1.添加学员')
    print('             2.查询学员')
    print('             3.修改学员')
    print('             4.删除学员')
    print('             0.退出程序')

    select = input('         请选择您的操作:')
    select = int(select)
    while select < 0 or select > 4:
        select = input('         选项有误,请重选:')
        select = int(select)

    if select == 1:

        add_stu()
    elif select == 2:

        query()
    elif select == 3:

        modify_stu()
    elif select == 4:

        del_stu()
    else:
        print('*        感谢您的使用,下次再会~        *')
        break

                                         正文  类和对象

"""
面向过程 C
面向对象 Java Javascript OC Python....
面向对象编程有三个特性:封装、继承、多态
"""

# 什么是类?
# 具有相同属性和行为方法的事务的抽象集合
# 人类、动物类、植物类....
# 什么是对象?
# 类的具体的实例
# 你自己、你家养的小黑狗


# class 类名(继承的父类)
# object 类  Python中的顶级父类(基类)
class People(object):

    # 初始化函数,是创建对象的时候自动调用的
    def __init__(self, name, sex):
        # 给self添加属性,self就是创建出来的对象
        # 对象.属性名 = 属性值   设置属性值
        self.name = name
        self.sex = sex
        print('People类型的对象被创建了')

    # 定义一个吃饭的功能
    def eat(self, food):
        """
        :param food: 食物名称
        :return: None
        """
        # self 对象A执行eat()函数,self指对象A  对象B执行eat() self指对象B
        print('%s 执行了吃饭的功能,吃了%s' % (self.name, food))

    def run(self, time):

        print('%s 跑了 %s 分钟' % (self.name, time))


# 创建People类的对象
# 对象名 = 类名()
p1 = People(name='张三', sex='男')
print(p1.name)
print(p1.sex)
# 修改对象原有属性的值
p1.name = '张三丰'
print(p1.name)
# 给对象p1添加一个sport属性,这属性只有p1有
p1.sport = '篮球'
print(p1.sport)

# 对象执行函数
print(p1)
p1.eat('鱼香肉丝')
p1.run(50)


p2 = People(name='李四', sex='女')
print(p2.name)
print(p2.sex)
p2.love = '跳舞'
print(p2)
p2.eat('宫保鸡丁')
p2.run(30)

                                            练习 

"""
花类 Flower
属性:花名 颜色  味道  花语  花季 花瓣数....
函数:爱不爱我、泡茶.....
"""

猜你喜欢

转载自blog.csdn.net/qq_40566294/article/details/81213325