以下是一个实现上述要求的 Python 代码:

# 学生信息列表
students = [
    {'姓名': '张三', '性别': '男', '年龄': 18},
    {'姓名': '李四', '性别': '女', '年龄': 19},
    {'姓名': '王五', '性别': '男', '年龄': 20},
    {'姓名': '赵六', '性别': '女', '年龄': 18},
    {'姓名': '钱七', '性别': '男', '年龄': 19},
    {'姓名': '孙八', '性别': '女', '年龄': 20},
    {'姓名': '周九', '性别': '男', '年龄': 18},
    {'姓名': '吴十', '性别': '女', '年龄': 19},
    {'姓名': '郑十一', '性别': '男', '年龄': 20}
]

# 班级学生信息字典
classrooms = {
    '班级1': students[:3],
    '班级2': students[3:6],
    '班级3': students[6:]
}

# 输入班级名
class_name = input('请输入班级名:')

# 输出班级学生信息
if class_name in classrooms:
    print(f'{class_name} 的学生信息:')
    for student in classrooms[class_name]:
        print(f'姓名:{student['姓名']},性别:{student['性别']},年龄:{student['年龄']}')
else:
    print('班级名不存在!')

运行以上代码,当输入班级名时,会输出该班级的学生信息。例如,当输入'班级1'时,输出如下:

请输入班级名:班级1
班级1 的学生信息:
姓名:张三,性别:男,年龄:18
姓名:李四,性别:女,年龄:19
姓名:王五,性别:男,年龄:20
Python 编程实现:字典保存班级学生信息并根据输入查询

原文地址: http://www.cveoy.top/t/topic/dP3u 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录