请帮我编写一个”课程表查询小工具预先输入的课程表,然后可以查看整个课程表,也可以选择查看某天或查看某天那个时间段的课程“的代码
时间: 2024-03-02 19:52:31 浏览: 21
好的,我可以帮你编写这个小工具的代码。以下是一个示例代码,可以供参考:
```python
class Course:
def __init__(self, name, day, start_time, end_time):
self.name = name
self.day = day
self.start_time = start_time
self.end_time = end_time
class CourseTable:
def __init__(self):
self.courses = []
def add_course(self, name, day, start_time, end_time):
course = Course(name, day, start_time, end_time)
self.courses.append(course)
def get_all_courses(self):
return self.courses
def get_courses_by_day(self, day):
return [c for c in self.courses if c.day == day]
def get_courses_by_time(self, time):
return [c for c in self.courses if c.start_time <= time <= c.end_time]
# 示例使用
table = CourseTable()
table.add_course('Math', 'Monday', 9, 11)
table.add_course('English', 'Monday', 13, 15)
table.add_course('Science', 'Wednesday', 10, 12)
# 查看整个课程表
print("All courses:")
for course in table.get_all_courses():
print(course.name, course.day, course.start_time, course.end_time)
# 查看星期一的课程
print("\nMonday's courses:")
for course in table.get_courses_by_day('Monday'):
print(course.name, course.start_time, course.end_time)
# 查看上午 10 点的课程
print("\nCourses at 10:")
for course in table.get_courses_by_time(10):
print(course.name, course.day)
```
这段代码使用了两个类:`Course` 表示一个课程,包含课程名、星期几、开始时间和结束时间等信息;`CourseTable` 表示整个课程表,包含一系列课程并提供查询功能。你可以根据自己的需求和场景进行相应的修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)