Python | 练习作业 2

为学生登录系统新增搜索功能。

第二天作业的解题思路:

1.创建一个空列表保存搜索结果

2.让用户输入要搜索的内容

3.遍历学生信息,检查学生的id name age gender score

中的属性值 是否跟用户搜索的内容一致

4.如果有一致的属性 那么就将该学生信息添加到搜索结果列表中

5.打印搜索结果

python 复制代码
#               学生管理系统-主页
# - * - * - * - * - * - * - * - * - * - * - * - * - * -  
#           1.录入学员信息(单条学员信息使用字典保存)(所有学员信息保存在一个列表里) 
#           2.删除学员信息     
#           3.查询所有学员
#           4.退出系统
# - * - * - * - * - * - * - * - * - * - * - * - * - * - 
#
stu_list = []
while True:
    print("-*-*-*-*-*-*-*-*-*")
    print("1.录入学员信息")
    print("2.删除学员信息")
    print("3.查询所有学员")
    print("-*-*-*-*-*-*-*-*-*")
    select = input("请输入你的选择:")
    if select == "1": #  "录入学员信息"
        id = input("请输入学员学号:")
        name = input("请输入学员姓名:")
        age = input("请输入学员年龄:")
        gender = input("请输入学员性别:") 
        score = input("请输入学员得分:") 
        stu = {"id":id,"name":name,"age":age,"gender":gender,"score":score} 
        stu_list.append(stu)
         input("输入回车返回菜单:")
    elif select == "2":#      "2.根据学号删除指定学员信息"
        # 使用学员数据删除学员
        # del_id = input("请输入要删除的学员学号:")
        # for stu in stu_list:
        #     if del_id == stu.get("id"):
        #          stu_list.remove(stu) 
        #          break
        # else:
        #     print("该学号不存在") 
   
        # 使用序号删除学员
        del_id = input("请输入要删除的学员学号:")
        for i in range(len(stu_list)):
           stu = stu_list[i]
           if stu.get("id") == del_id:
              stu_list.pop(i)
              break
        else:
           print("要删除的学号不存在")
        input("要输入回车返回菜单:")
    elif select == "3":#     3.查询所有学员
        s = "学号\t姓名\t年龄\t性别\t得分\n"
        for stu in stu_list:
          s += f"{stu['id']}\t{stu['name']}\t{stu['age']}\t{stu['gender']}\t{stu['score']}\n" 
        input("输入回车返回菜单:")
    elif select == "4":
        # 1.创建一个空列表保存搜索结果
        search_result = []    
        # 2.让用户输入要搜索的内容
        data = input("请输入搜索的内容")
        # 3.遍历学生信息,检查学生的id name age gender score中的属性值 是否跟用户搜索的内容一致
        for stu in stu_list:
           for value in stu.values():
              # 4.如果有一致的属性 那么就将该学生信息添加到搜索结果列表中
                if value == data:
                   search_result.append(stu)
                   break
        # 5.打印搜索结果
        print(search_result)
        input("输入回车返回菜单:")        
相关推荐
Sylvia-girl1 小时前
Java——抽象类
java·开发语言
Yana.nice3 小时前
Bash函数详解
开发语言·chrome·bash
江沉晚呤时4 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
xchenhao4 小时前
基于 Flutter 的开源文本 TTS 朗读器(支持 Windows/macOS/Android)
android·windows·flutter·macos·openai·tts·朗读器
电脑能手5 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
tomorrow.hello5 小时前
Java并发测试工具
java·开发语言·测试工具
Edward-tan5 小时前
CCPD 车牌数据集提取标注,并转为标准 YOLO 格式
python
晓13135 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊5 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
倔强青铜三6 小时前
苦练Python第18天:Python异常处理锦囊
人工智能·python·面试