笔记三:学员管理系统(简单版本)(列表、字典)

下面是两个简单版本的学员管理系统,不带存储。

1.第一种是用字典加列表结合起来写出来的,把一个学员的全部信息放入一个字典里,再把放学员的信息的字典放入列表中作为一个元素,这样的话,每个列表中的每个元素即为每个学员的信息,即:[{'name':'张三',age:20……},{'name':'李四',age:20……},{'name':'王五',age:20……},{'name':'刘六',age:20……},{'name':'小娴',age:20……}……],然后再对列表进行一个学员的增删改查和列表中字典的学员信息的增删改查!

下面是代码过程实现!

# -*- coding: utf-8 -*-
__author__ = 'wj'
__date__ = '2018/7/10 14:04'


def add_stu():
    """
    添加学员
    :return: None
    """
    while True:
        name = input('*     请输入学员姓名:')
        age = input('*     请输入学员年龄:')
        phone = input('*     请输入学员电话:')

        # 将三条信息组装为一个字典
        stu = {'name': name, 'age': age, 'phone': phone}

        # 将小字典放入大列表中
        students.append(stu)

        is_next = input('回车继续添加,输入q结束:')
        if is_next == 'q':

            break


def query_all_stu():
    """
    查询所有学员信息
    :return: None
    """
    # 判断是否有学员信息
    if len(students) == 0:
        print('*        没有学员信息,请稍后重试~   ')
        return
    # 遍历所有学员信息
    print('*    查询结果如下:')
    # enumerate()  将列表进行枚举,会得到索引和对应位置的元素组成的元组
    # idx, stu 将两个变量和元组中的数据一一对应
    for idx, stu in enumerate(students):
        # idx 是小字典的索引   stu是小字典
        print('*    索引:%s    姓名:%s   年龄:%s   电话:%s ' % (idx + 1, stu['name'], stu['age'], stu['phone']))


def query_of_keyword():
    """
    根据关键词搜索学员信息
    :return: None
    """
    # 判断是否有学员信息
    if len(students) == 0:
        print('*        没有学员信息,请稍后重试~   ')
        return

    # 输入一个关键词
    kw = input('*   请输入要搜索的关键词:')
    # 声明变量用来记录找到的个数
    count = 0
    for idx, stu in enumerate(students):

        # 判断关键词是否在学员姓名中
        if kw in stu['name']:
            count += 1
            print('*    索引:%s   姓名:%s   年龄:%s   电话:%s' % (idx + 1, stu['name'], stu['age'], stu['phone']))

    # 判断是否找到结果
    if count == 0:
        print('*        没有找到结果~ ')

    else:
        print('*        共找到%s个结果!   ' % count)


def query_stu():
    """
    查询学员
    :return: None
    """
    print('*    a.关键字查找')
    print('*    b.查找所有学员')

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

    if select == 'a':
        query_of_keyword()

    else:
        query_all_stu()


def modify_stu():
    """
    修改学员信息
    :return:None
    """
    if len(students) == 0:
        print('*    没有学员信息,无法执行修改操作!')
        return
    # 1.展示所有学员信息
    query_all_stu()
    # 2.选择要修改的学员索引,判断索引是否在范围
    idx = int(input('*    请选择要修改的学员索引:'))
    while idx < 1 or idx > len(students):
        idx = int(input('*    索引有误,请重选:'))

    # 3.根据索引取出小字典
    stu = students[idx - 1]

    stu['name'] = input('*    请输入修改后的姓名(%s):' % stu['name'])
    stu['age'] = input('*    请输入修改后的年龄(%s):' % stu['age'])
    stu['phone'] = input('*    请输入修改后的电话(%s):' % stu['phone'])

    print('*    修改完成!')


def delete_stu():
    """
    删除学员
    :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':
        # 1.展示所有学员信息
        query_all_stu()
        # 2.选择要删除的索引
        idx = int(input('*    输入要删除的学员索引:'))
        while idx < 1 or idx > len(students):
            idx = int(input('*    索引有误,请重选:'))
        # 3.取出选择的数据,确认是否删除
        stu = students[idx - 1]
        is_del = input('*   确认要删除(%s)?y/n:' % stu['name'])
        if is_del == 'y':
            del students[idx - 1]
            print('*    删除成功!')
    else:
        # 删除列表中所有数据
        # del students[:]
        while len(students):
            students.pop()
        print('*    删除成功!')


# 存储所有学员信息的大列表
students = []


while True:

    print('*********智游学员管理V1.0*********')
    print('*          1.添加学员            *')
    print('*          2.修改学员            *')
    print('*          3.删除学员            *')
    print('*          4.查询学员            *')
    print('*          0.退出程序            *')
    select = int(input('        请选择您的操作:'))

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

    print('**********************************')

    if select == 1:
        add_stu()

    elif select == 2:
        modify_stu()

    elif select == 3:
        delete_stu()

    elif select == 4:
        query_stu()

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







1.第二种是用列表写出来的,把一个学员的全部信息放入一个小列表里,再把放学员的信息的列表放入大列表中作为一个元素,这样的话,每个列表中的每个元素即为每个学员的信息,即:[['name':'张三',age:20……],['name':'李四',age:20……],['name':'王五',age:20……],['name':'刘六',age:20……],['name':'小娴',age:20……]……],然后再对大列表进行一个学员的增删改查和小列表中字典的学员信息的增删改查!

下面是代码过程实现!

# -*- coding: utf-8 -*-
__author__ = 'wj'
__date__ = '2018/7/7 16:08'


def add_stu():
    """
    添加学员
    :return: None
    """
    # 循环添加
    while True:
        name = input('*     请输入要添加的姓名:')
        age = input('*     请输入要添加的年龄:')
        phone = input('*     请输入要添加的电话:')
        # 将三条数据组成一个小列表
        stu = [name, age, phone]
        # 将小列表添加到大列表中
        students.append(stu)

        is_next = input('是否继续添加?y/n:')

        if is_next != 'y':
            # 结束循环
            break


def query_all_stu():
    """
    查询所有学员信息
    :return: None
    """
    if len(students) == 0:
        print('*    没有学员信息,请稍后再查~   *')
        # return 1.强制结束函数执行,return之后的代码不会再执行
        return

    # 遍历查找所有的学员信息
    print('*            查找结果如下:         *')
    for x in range(0, len(students)):
        # stu就是 [姓名, 年龄, 电话]小列表
        stu = students[x]
        print('*    索引:%s    姓名:%s   年龄:%s   电话:%s' % (x+1, stu[0], stu[1], stu[2]))


def query_of_keyword():
    """
    根据姓名关键词查找学员
    :return:None
    """
    if len(students) == 0:

        print('*     没有学员信息,请稍后再查~   *')
        return
    # 1.输入搜索的关键词
    kw = input('*   请输入要搜索的关键词:')

    print('*            查找结果如下:         *')
    # 统计查找到的结果条数
    count = 0
    # 2.遍历大列表
    for x in range(0, len(students)):
        # stu就是[姓名,年龄,电话] 小列表
        stu = students[x]
        # 判断stu小列表中的姓名
        if kw in stu[0]:
            # 让计数+1
            count += 1

            print('*    索引:%s   姓名:%s   年龄:%s   电话:%s' % (x+1, stu[0], stu[1], stu[2]))

    # 3.如果count在循环结束之后依然是0,说明没有找到符合条件的结果
    if count == 0:
        print('*             无结果!              *')
    else:
        print('*        共搜索到%s条数据 !        *' % count)


def query_stu():
    """
    查询学员功能函数
    :return:None
    """
    print('*    a.查询所有学员')
    print('*    b.输入关键词查询')
    select = input('*   请选择查询方式:')

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

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


def modify_stu():
    """
    修改学员信息
    :return:None
    """
    if len(students) == 0:

        print('*    没有学员信息,无法执行修改操作~    *')
        return

    # 1.查询所有学员信息
    query_all_stu()

    # 2.选择修改的学员序号,判断索引是否在范围
    idx = int(input('*    请选择要修改的学员索引:'))
    while idx < 1 or idx > len(students):
        idx = int(input('*    索引有误,请重选:'))

    # 3.根据索引修改对应的数据
    stu = students[idx - 1]
    new_name = input('*    请输入修改后的学员姓名(%s):' % stu[0])
    new_age = input('*    请输入修改后的学员年龄(%s):' % stu[1])
    new_phone = input('*    请输入修改后的学员电话(%s):' % stu[2])
    # 修改信息
    stu[0] = new_name
    stu[1] = new_age
    stu[2] = new_phone

    print('*        信息修改成功 !')


def delete_stu():
    """
    删除学员信息
    :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':
        # 1.查询所有学员信息
        query_all_stu()

        # 2.选择要删除的索引,判断索引是否在范围
        idx = int(input('*    选择要删除学员的索引:'))
        while idx < 1 or idx > len(students):
            idx = int(input('*    索引有误,请重选:'))

        # 3.提醒是否删除
        stu = students[idx - 1]
        is_del = input('*    确认要删除(%s)?y/n:' % stu[0])

        if is_del == 'y':
            # 4.删除学员
            del students[idx - 1]
            print('*    删除操作执行成功 !')

    else:
        is_del = input('*    确认删除所有学员?y/n:')
        if is_del == 'y':
            # 删除所有学员信息
            # del students[:]
            while len(students):
                students.pop()
            print('*    删除操作执行成功 !')


# 存储所有学员信息的大列表
students = []


# True(可以用数字1表示) False(可以用数字0表示) 布尔类型数据
while True:

    print('*********智游学员管理V1.0*********')
    print('*          1.添加学员            *')
    print('*          2.修改学员            *')
    print('*          3.删除学员            *')
    print('*          4.查询学员            *')
    print('*          0.退出程序            *')
    select = int(input('        请选择您的操作:'))

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

    print('**********************************')

    if select == 1:
        add_stu()

    elif select == 2:
        modify_stu()

    elif select == 3:
        delete_stu()

    elif select == 4:
        query_stu()

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



以上代码若有错误,请在评论区指教!谢谢!

猜你喜欢

转载自blog.csdn.net/qq_41082423/article/details/81270961