Final Project: Build a More Complex Python App from Scratch

In this final article of our beginner-friendly Python tutorial series, we'll build a more complex Python app from scratch, incorporating various concepts you've learned throughout the series, such as data structures, functions, classes, file handling, and user interaction. We'll create a simple task management system that allows users to add, view, update, delete, and save tasks to a file.

Defining the Task Class

Let's begin by defining a `Task` class representing a task in our task management system. The class will have attributes for the task's title, description, and completion status, as well as methods for displaying and updating the task's information:

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

    def display(self):
        print(f"Title: {self.title}")
        print(f"Description: {self.description}")
        print(f"Completed: {self.completed}")

    def update(self, title, description, completed):
        self.title = title
        self.description = description
        self.completed = completed
    

Building the Task Management Functions

Next, let's create functions for adding, viewing, updating, deleting, and saving tasks. We'll store tasks in a list and use unique IDs to reference them:

import json

tasks = []
next_id = 1

def add_task(title, description):
    global next_id
    new_task = Task(title, description)
    tasks.append((next_id, new_task))
    next_id += 1

def view_task(task_id):
    for id, task in tasks:
        if id == task_id:
            task.display()
            return
    print("Task not found.")

def update_task(task_id, title, description, completed):
    for id, task in tasks:
        if id == task_id:
            task.update(title, description, completed)
            print("Task updated successfully.")
            return
    print("Task not found.")

def delete_task(task_id):
    global tasks
    original_length = len(tasks)
    tasks = [(id, task) for id, task in tasks if id != task_id]
    if original_length != len(tasks):
        print("Task deleted successfully.")
    else:
        print("Task not found.")

def save_tasks_to_file(filename):
    task_data = [{"id": id, "title": task.title, "description": task.description, "completed": task.completed} for id, task in tasks]
    with open(filename, "w") as file:
        json.dump(task_data, file)

def load_tasks_from_file(filename):
    global tasks, next_id
    try:
        with open(filename, "r") as file:
            task_data = json.load(file)
            tasks = [(task["id"], Task(task["title"], task["description"], task["completed"])) for task in task_data]
            next_id = max(task["id"] for task in task_data) + 1
    except FileNotFoundError:
        print(f"File '{filename}' not found. Starting with an empty task list.")

User Interface and Main Loop

Now, let's create a user interface for our task management system. The interface will display a menu and prompt the user to select an action. We'll use a while loop to keep the program running until the user chooses to exit:

def print_menu():
    print("1. Add Task")
    print("2. View Task")
    print("3. Update Task")
    print("4. Delete Task")
    print("5. Save Tasks to File")
    print("6. Load Tasks from File")
    print("7. Exit")

filename = "tasks.json"
load_tasks_from_file(filename)

while True:
    print_menu()
    choice = int(input("Enter your choice: "))
    
    if choice == 1:
        title = input("Enter the task title: ")
        description = input("Enter the task description: ")
        add_task(title, description)
        print("Task added successfully.")
    elif choice == 2:
        task_id = int(input("Enter the task ID: "))
        view_task(task_id)
    elif choice == 3:
        task_id = int(input("Enter the task ID: "))
        title = input("Enter the new task title: ")
        description = input("Enter the new task description: ")
        completed = input("Is the task completed? (yes/no): ").lower() == "yes"
        update_task(task_id, title, description, completed)
    elif choice == 4:
        task_id = int(input("Enter the task ID: "))
        delete_task(task_id)
    elif choice == 5:
        save_tasks_to_file(filename)
        print("Tasks saved to file.")
    elif choice == 6:
        load_tasks_from_file(filename)
        print("Tasks loaded from file.")
    elif choice == 7:
        print("Exiting the application.")
        break
    else:
        print("Invalid choice. Please try again.")
        
    print("\n")

 

This final project combines various Python concepts covered throughout the tutorial series. By building a more complex task management system, you will gain hands-on experience with data structures, functions, classes, file handling, and user interaction. Use this project as a foundation to further explore Python and develop more complex applications. Good luck, and happy coding!

Table of Contents: Python for Beginners

  1. Getting Started with Python: Installation and Basics
  2. Python Data Types and Variables: An Introduction
  3. Python Conditionals and Loops: Control Your Code
  4. Python Functions: Organize and Reuse Your Code
  5. Python Modules and Packages: Manage and Organize Your Projects
  6. Python File Handling: Read and Write Files
  7. Python Error Handling: Using Try and Except
  8. Python Data Structures: Lists, Tuples, Sets, Dictionaries
  9. Python Object-Oriented Programming: Classes and Objects
  10. Final Project: Build a More Complex Python App from Scratch