Task Management

This  Task Management Program is designed to store and manage tasks dynamically. It supports basic CRUD operations — Create, Read, Update, and Delete. The program allows users to add new tasks, mark tasks as completed, and view pending ones, making task tracking simple and efficient.

#Code

import os


FILENAME = "todo_list.txt"


def load_tasks():

    if not os.path.exists(FILENAME):

        return []

    with open(FILENAME, 'r') as f:

        return [line.strip() for line in f.readlines()]


def save_tasks(tasks):

    with open(FILENAME, 'w') as f:

        for task in tasks:

            f.write(task + '\n')


def show_tasks(tasks):

    if not tasks:

        print("No tasks yet!")

    else:

        print("\nYour Tasks:")

        for i, task in enumerate(tasks, start=1):

            print(f"{i}. {task}")

    print()


def add_task(tasks):

    task = input("Enter a new task: ")

    tasks.append(task)

    print("Task added!")


def delete_task(tasks):

    show_tasks(tasks)

    try:

        num = int(input("Enter task number to delete: "))

        if 1 <= num <= len(tasks):

            removed = tasks.pop(num - 1)

            print(f"Removed: {removed}")

        else:

            print("Invalid number.")

    except ValueError:

        print("Please enter a valid number.")


def main():

    tasks = load_tasks()


    while True:

        print("\n--- To-Do List Menu ---")

        print("1. View tasks")

        print("2. Add task")

        print("3. Delete task")

        print("4. Save and Exit")


        choice = input("Choose an option: ")


        if choice == '1':

            show_tasks(tasks)

        elif choice == '2':

            add_task(tasks)

        elif choice == '3':

            delete_task(tasks)

        elif choice == '4':

            save_tasks(tasks)

            print("Tasks saved. Goodbye!")

            break

        else:

            print("Invalid choice. Try again.")


if __name__ == "__main__":

    main() 



#Output




Comments

Popular posts from this blog

Python Code

CV