Your program has the following requirements: Add students, remove students, and view students in a unit. • Add units, remove units, and view units in a degree. Testing you can: To complete this part of the assignment you are required to write the code that demonstrates that Add a student to a unit, remove a student from a unit, and view all students in the unit. Add a unit to a degree, remove a unit from a degree, and view all units in the degree.
时间: 2025-03-09 08:06:48 浏览: 32
好的,按照您的需求,我们可以设计一个Python程序来模拟学生管理系统的部分核心功能。我们将实现以下几个关键点:
1. **添加、删除和查看单位内(unit)的学生**
2. **添加、删除和查看学位项目(degree)内的单位**
### 实现思路
- 我们将创建三个类:`Student`, `Unit` 和 `Degree`.
- 每个 `Unit` 对象都会有一个内部的 `students` 列表用于存放它所属的所有 `Student` 对象。
- 类似地,每个 `Degree` 对象会持有一个 `units` 字典来追踪其包含的所有 `Unit`.
下面是具体的 Python 代码实现:
```python
class Student:
def __init__(self, id, name):
self.id = id
self.name = name
def display(self):
return f"Student ID: {self.id}, Name: {self.name}"
class Unit:
def __init__(self, unit_code, description=''):
self.unit_code = unit_code
self.description = description
self.students = []
def add_student(self, student):
""" Adds a student to the unit """
self.students.append(student)
print(f'Student added successfully!')
def remove_student(self, student_id):
""" Removes a student based on their ID"""
initial_length = len(self.students)
self.students = [s for s in self.students if s.id != student_id]
if len(self.students) < initial_length:
print('Student removed successfully!')
else:
print('No matching student found.')
def list_students(self):
""" Lists all students within this unit."""
if not self.students:
print("This unit does not contain any students yet.")
else:
for stu in self.students:
print(stu.display())
class Degree:
def __init__(self, degree_name):
self.degree_name = degree_name
self.units = {}
def add_unit(self, unit_code, description=''):
""" Adds a new unit with optional descriptive text under this degree """
if unit_code not in self.units:
new_unit = Unit(unit_code, description)
self.units[unit_code] = new_unit
print(f'New unit "{unit_code}" created and added to degree "{self.degree_name}".')
else:
print(f'A unit named "{unit_code}" already exists in this degree.')
def remove_unit(self, unit_code):
""" Deletes an existing unit by its unique code string """
try:
del self.units[unit_code]
print(f'The unit "{unit_code}" was deleted.')
except KeyError:
print(f'There is no such unit called "{unit_code}"')
def list_units(self):
""" Outputs every registered unit alongside brief information about them """
if not self.units:
print("There are currently no units listed in this degree.")
else:
for uc, ud in self.units.items():
print(f"\nCode: {uc}\nDescription: {ud.description}")
# 测试示例
if __name__ == "__main__":
compsci_degree = Degree('Computer Science') # 创建计算机科学学位
web_dev_unit = "WEBD"
ai_ml_unit = "AIML"
# 向 Computer Science 中增加 Units
compsci_degree.add_unit(web_dev_unit,"Web Development Fundamentals")
compsci_degree.add_unit(ai_ml_unit,"Artificial Intelligence & Machine Learning")
alice = Student(id='A001', name="Alice Smith") # 定义几个测试用的学生实体...
bob = Student(id='B007', name="Bob Brown")
# 将 Alice 加入 Web 开发基础课单元中
compsci_degree.units[web_dev_unit].add_student(alice)
# 打印当前选修了该课程的所有人
print("\nStudents enrolled in Web Dev:")
compsci_degree.units[web_dev_unit].list_students()
# 再次尝试向 WEBDEV 添加 Bob 并列出所有学员名单
compsci_degree.units[web_dev_unit].add_student(bob)
print("\nUpdated Students List after adding another one:")
compsci_degree.units[web_dev_unit].list_students()
# 展示 AI&ML 的信息但是还没有加入任何学生进去
print('\nInformation About AIML:')
compsci_degree.list_units()[ai_ml_unit]
# 移除刚刚添加的一个学生然后再次展示结果
compsci_degree.units[web_dev_unit].remove_student('B007')
print("\nAfter removing someone:")
compsci_degree.units[web_dev_unit].list_students()
# 清理工作 - 移掉刚才建立的那个度项目下的所有资料
compsci_degree.remove_unit(web_dev_unit)
compsci_degree.remove_unit(ai_ml_unit)
```
以上脚本演示了一个简化版本的学生管理系统的核心功能。通过这些例子可以看到如何轻松地对数据结构进行操作以满足题目提出的条件。
阅读全文