Loading

Quipoin Menu

Learn • Practice • Grow

python / Final Project
tutorial

Final Project

Congratulations! You've learned the core concepts of Python. Now it's time to build a complete project that brings everything together. We'll create a Task Manager CLI Application that uses:
  • Object‑Oriented Programming (classes)
  • File I/O to store tasks
  • Exception handling
  • Functions and modules
  • List comprehensions and control flow

Project Requirements
Users should be able to:
- Add a new task (with description and due date)
- View all tasks
- Mark a task as complete
- Delete a task
- Save tasks to a file and load them on startup

Step 1: Define the Task Class
We'll create a `Task` class to represent each task.


import datetime

class Task:
def __init__(self, description, due_date):
self.description = description
self.due_date = due_date
self.completed = False

def __str__(self):
status = "[X]" if self.completed else "[ ]"
return f"{status} {self.description} (due: {self.due_date})"

Step 2: Task Manager Class
Manages the list of tasks and handles persistence.


import json

class TaskManager:
def __init__(self, filename="tasks.json"):
self.filename = filename
self.tasks = []
self.load_tasks()

def add_task(self, description, due_date):
try:
# Validate due_date format
due = datetime.datetime.strptime(due_date, "%Y-%m-%d").date()
task = Task(description, due)
self.tasks.append(task)
return True
except ValueError:
return False

def view_tasks(self, show_completed=True):
for i, task in enumerate(self.tasks, 1):
if show_completed or not task.completed:
print(f"{i}. {task}")

def mark_complete(self, index):
if 1 <= index <= len(self.tasks):
self.tasks[index-1].completed = True
return True
return False

def delete_task(self, index):
if 1 <= index <= len(self.tasks):
self.tasks.pop(index-1)
return True
return False

def save_tasks(self):
data = [{"description": t.description, "due_date": t.due_date.isoformat(), "completed": t.completed} for t in self.tasks]
with open(self.filename, "w") as f:
json.dump(data, f, indent=2)

def load_tasks(self):
try:
with open(self.filename, "r") as f:
data = json.load(f)
for item in data:
due = datetime.datetime.fromisoformat(item["due_date"]).date()
task = Task(item["description"], due)
task.completed = item["completed"]
self.tasks.append(task)
except (FileNotFoundError, json.JSONDecodeError):
# No saved tasks or corrupted file – start fresh
pass

Step 3: Command‑Line Interface
Put everything together with a simple menu.


def main():
manager = TaskManager()
while True:
print("n=== Task Manager ====")
print("1. Add Task")
print("2. View Tasks")
print("3. Mark Task Complete")
print("4. Delete Task")
print("5. Save & Exit")
choice = input("Choose an option: ").strip()

if choice == "1":
desc = input("Description: ")
due = input("Due date (YYYY-MM-DD): ")
if manager.add_task(desc, due):
print("Task added.")
else:
print("Invalid date format. Use YYYY-MM-DD.")

elif choice == "2":
manager.view_tasks()

elif choice == "3":
manager.view_tasks()
try:
idx = int(input("Task number to mark complete: "))
if manager.mark_complete(idx):
print("Task marked complete.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a number.")

elif choice == "4":
manager.view_tasks()
try:
idx = int(input("Task number to delete: "))
if manager.delete_task(idx):
print("Task deleted.")
else:
print("Invalid task number.")
except ValueError:
print("Please enter a number.")

elif choice == "5":
manager.save_tasks()
print("Tasks saved. Goodbye!")
break

else:
print("Invalid option. Try again.")

if __name__ == "__main__":
main()

Step 4: Run and Test
Save the code in a file (e.g., `task_manager.py`) and run it. Try adding tasks, viewing them, marking complete, and exiting. The tasks will be saved to `tasks.json` and reloaded next time.

This project uses all the concepts you've learned: classes, file I/O, exception handling, list comprehensions, and user interaction. You can extend it further by adding features like due‑date sorting, searching, or a graphical interface.
Two Minute Drill
  • This capstone project ties together OOP, file I/O, exception handling, and CLI design.
  • Tasks are stored in a JSON file for persistence.
  • The CLI provides a menu‑driven interface.
  • You can extend the project with new features to practice more.
  • Congratulations on completing the Python tutorial!

Need more clarification?

Drop us an email at career@quipoinfotech.com