宿舍管理系统python代码
时间: 2024-06-23 20:02:38 浏览: 156
宿舍管理系统源代码
创建一个宿舍管理系统(如学生宿舍分配、信息查询等)的Python代码示例可能包括多个模块和功能,这里我会提供一个简单的框架。请注意,实际的代码会更复杂,可能需要数据库连接、用户界面或者web服务。这里是一个基本的文本控制版本:
```python
class Dormitory:
def __init__(self, rooms):
self.rooms = rooms
self.students = {}
def add_student(self, student_id, room_no):
if room_no in self.rooms:
self.students[student_id] = room_no
print(f"{student_id} added to room {room_no}")
else:
print("Room not available.")
def remove_student(self, student_id):
if student_id in self.students:
room_no = self.students.pop(student_id)
print(f"{student_id} removed from room {room_no}")
else:
print(f"{student_id} not found in the system.")
def list_students(self, room_no=None):
if room_no is None:
for student_id, room in self.students.items():
print(f"Student {student_id} is in room {room}")
else:
students_in_room = [student_id for student_id, room in self.students.items() if room == room_no]
print(f"Students in room {room_no}: {students_in_room}")
# 示例使用
dorm = Dormitory(["Room1", "Room2", "Room3"])
dorm.add_student(1001, "Room1")
dorm.add_student(1002, "Room2")
dorm.list_students()
dorm.remove_student(1001)
阅读全文