Python1. 实验内容: 设计一个简单的学生信息管理系统,学生信息包括学号、姓名、年龄、性别、专业、学院等。要求实现以下功能: (1)学生信息录入功能; (2)学生信息浏览功能; (3)学生信息查询功能,按学号、姓名查询; (4)学生信息的删除与修改。
时间: 2024-11-22 20:39:51 浏览: 8
好的,以下是使用Python实现一个简单的学生信息管理系统的步骤和代码示例:
```python
class Student:
def __init__(self, sid, name, age, gender, major, college):
self.sid = sid
self.name = name
self.age = age
self.gender = gender
self.major = major
self.college = college
class SchoolManagementSystem:
def __init__(self):
self.students = []
# 学生信息录入
def add_student(self, student_data):
new_student = Student(*student_data)
self.students.append(new_student)
# 学生信息浏览
def display_students(self):
for i, student in enumerate(self.students, start=1):
print(f"ID: {i}, Name: {student.name}, Age: {student.age}, Gender: {student.gender}, Major: {student.major}, College: {student.college}")
# 学生信息查询
def search_student(self, query_type, query_value):
if query_type == "sid":
return next((s for s in self.students if s.sid == query_value), None)
elif query_type == "name":
return next((s for s in self.students if s.name == query_value), None)
else:
print("Invalid query type. Please use 'sid' or 'name'.")
# 学生信息删除
def remove_student(self, sid):
self.students = [s for s in self.students if s.sid != sid]
print(f"Student with ID {sid} has been removed.")
# 学生信息修改
def update_student(self, sid, updated_data):
for student in self.students:
if student.sid == sid:
student.name = updated_data.get('name', student.name)
student.age = updated_data.get('age', student.age)
student.gender = updated_data.get('gender', student.gender)
student.major = updated_data.get('major', student.major)
student.college = updated_data.get('college', student.college)
break
else:
print("No student found with the given ID.")
# 实例化并测试系统
sms = SchoolManagementSystem()
sms.add_student(["1", "张三", 20, "男", "计算机科学", "计算机系"])
sms.add_student(["2", "李四", 22, "女", "软件工程", "软件学院"])
sms.display_students()
search_result = sms.search_student("sid", 1)
print(search_result)
sms.update_student(1, {"name": "张新", "age": 21})
sms.remove_student(2)
```
阅读全文