小学排课 Python 源码示例 - 简洁实用
以下是一个简单的实用小学排课程的 Python 源码示例:
class Course:
def __init__(self, name, duration):
self.name = name
self.duration = duration
class Timetable:
def __init__(self, courses):
self.courses = courses
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
self.courses.remove(course)
def get_total_duration(self):
total_duration = 0
for course in self.courses:
total_duration += course.duration
return total_duration
def print_timetable(self):
print('Timetable:')
for course in self.courses:
print(f'{course.name} ({course.duration} minutes)')
# 创建课程对象
math_course = Course('Math', 60)
science_course = Course('Science', 45)
english_course = Course('English', 30)
# 创建课程表对象
timetable = Timetable([math_course, science_course, english_course])
# 打印课程表
timetable.print_timetable()
# 添加新课程
art_course = Course('Art', 45)
timetable.add_course(art_course)
# 打印更新后的课程表
timetable.print_timetable()
# 移除课程
timetable.remove_course(math_course)
# 打印更新后的课程表
timetable.print_timetable()
# 计算总时长
total_duration = timetable.get_total_duration()
print(f'Total duration: {total_duration} minutes')
这个源码示例使用了两个类:Course和Timetable。Course类表示一门课程,具有课程名称和时长两个属性。Timetable类表示课程表,可以添加、移除课程,并计算总时长。
原文地址: https://www.cveoy.top/t/topic/qnPq 著作权归作者所有。请勿转载和采集!