StudyTracker: незалежні трекери
Приклад роботи з класом: стан екземпляра, винятки та обчислення прогресу.
Code and results
Published version after Merge. Results were saved by the author; code does not run on this page.
implementation.py · v1
class StudyTracker:
def __init__(self, student_name):
self.student_name = student_name
self.lessons = {}
def add_lesson(self, title):
if title in self.lessons:
raise ValueError("Заняття вже додане")
self.lessons[title] = False
def complete_lesson(self, title):
if title not in self.lessons:
raise KeyError(title)
self.lessons[title] = True
def progress(self):
total = len(self.lessons)
completed = sum(self.lessons.values())
return {"completed": completed, "total": total,
"percent": 100 * completed / total if total else 0}
def pending_lessons(self):
return [title for title, done in self.lessons.items() if not done]
first, second = StudyTracker("Олена"), StudyTracker("Андрій")
assert first.progress()["percent"] == 0
first.add_lesson("Списки")
first.add_lesson("Словники")
first.complete_lesson("Списки")
first.complete_lesson("Списки")
assert first.progress()["percent"] == 50
assert second.progress()["total"] == 0
assert first.pending_lessons() == ["Словники"]
Community reviews
Основна реалізація відповідає описаній ідеї. Додаткові приклади можна розвинути окремо.
