DEV Community

Cover image for A Simple Command-Line Task Manager in Python.
Lawani Elyon John
Lawani Elyon John

Posted on

A Simple Command-Line Task Manager in Python.

Table of contents

1.Setting up the development environment
2.Prerequisites
3.Project initialization
4.Implementing Features
5.Testing and Debugging
6.Final Project

In this tutorial, we’re going to build a simple command-line task manager using Python. Task managers are a great project for learning basic programming concepts such as user input, loops, conditionals, and lists. By the end of this tutorial, you'll have a working task manager where you can add, view, update, and delete tasks.

Setting up the development environment:

In order to complete this project choosing a programming language is important.
Programming languages are the fundamental foundations of software development, it helps in breaking down a series of logical steps known as algorithms.
Python is a popular programming language used for web development (server-side), software development, mathematics and system scripting.

Step 1: Before creating/building this project we need to make sure python is installed on your system You can download and install it from the official Python website: Python Downloads.
Make sure Python is installed on your system.

Python Development

Step 2: Next process in creating our program is having an IDE (Integrated Development Environment) this is a combination of tools like a code editor, compiler and debugger.
Install an IDE (such as Visual Studio Code) Visual Studio.

Image description

Visual studio will be used as the IDE for this project.
It is designed to help in creating software applications.

Project Initialization:

Now that your environment is ready, let's start by creating a new directory for our project. You can create this through the terminal or the file explorer. In your terminal, navigate to your project folder and create a new Python file:



mkdir task_manager
cd task_manager
touch task_manager.py



Enter fullscreen mode Exit fullscreen mode

This will serve as the main file where all our task management logic will be written.

Implementing the Features:

Adding Tasks:

You’ll start by creating a basic Command Line Interface that asks the user for his task title and description



tasks = []

def create_tasks():
    title = input("Enter Your Task Title: ")
    description = input("Enter Your Task Description: ")
    tasks.append({"Task Title": title, "Task Description": description})
    print("Task Created Successfully.")



Enter fullscreen mode Exit fullscreen mode

You created a function create task that prompts the user to enter his task title and his description, it will also display a success message indicating that you've created your task successfully.
The tasks list will store dictionaries, where each dictionary represents a task
Next, let's create an interactive loop that presents options to the user. This loop will allow the user to choose between adding, viewing, updating, and deleting tasks, or exiting the program



while True:
    print("\n Welcome to the interactive task manager..")
    print("1. Add Task")
    print("2. View Task")
    print("3. Update Task")
    print("4. Delete Task")
    print("5. Exit")

    choice = input("Select an option from (1-5): ")

    if choice == '1':
        create_tasks()
    elif choice == '2':
        see_tasks()
    elif choice == '3':
        update_tasks()
    elif choice == '4':
        delete_tasks()
    elif choice == '5':
        print("Exiting the task manager.")
        break
    else:
        print("Invalid option. Please select between 1 and 5.")



Enter fullscreen mode Exit fullscreen mode

Viewing Tasks

You're going to make sure that these tasks have been created successfully by viewing them



def see_tasks():
    if tasks:
        print("Available Tasks: ")
        for idx, task in enumerate(tasks, start=1):
            print(f"{idx}. Task Title: {task['Task Title']}, Task Description: {task['Task Description']}")
    else:
        print("No Tasks are Available...")


Enter fullscreen mode Exit fullscreen mode

Your function created allows the user to view their tasks, through looping each task in the list.

Updating Tasks:

In the update_tasks() function, we first display all the tasks so the user can decide which one they want to update. The user is asked to provide the index number of the task, and then they can either update the title, description, or both.



def update_tasks():
    see_tasks()
    if tasks:
        tasks_index = int(input("Provide the Index of your task to be updated: ")) - 1
        if 0 <= tasks_index < len(tasks):
            new_task_title = input("Provide your new title or (Press Enter to keep current title): ")
            new_task_description = input("Provide your new description or (Press Enter to keep current description): ")
            if new_task_title:
                tasks[tasks_index]['Task Title'] = new_task_title
            if new_task_description:
                tasks[tasks_index]['Task Description'] = new_task_description
            print("Your Task has been updated")
        else:
            print("Invalid Response. Please try again.")
    else:
        print("There are no tasks available.")



Enter fullscreen mode Exit fullscreen mode

This allows the user to update only the parts of the task they wish to change, making it a flexible update function.

Deleting Tasks

Sometimes, users may want to remove tasks that are no longer needed. We can enable this by allowing the user to delete a task based on its index. Here's how the delete_tasks() function works:



def delete_tasks():
see_tasks()
if tasks:
tasks_index = int(input("Provide the Index of the task to be deleted: ")) - 1
if 0 <= tasks_index < len(tasks):
deleted_task = tasks.pop(tasks_index)
print(f"Task '{deleted_task['Task Title']}' deleted successfully.")
else:
print("This is an invalid task index.")
else:
print("There are no tasks available.")

Enter fullscreen mode Exit fullscreen mode




Testing and Debugging

  1. Testing the Create Function:
    Add tasks by selecting the "Add Task" option from the menu.
    Check if the task is correctly stored and displayed when you use the "View Task" option.
    Testing the Update Function:

  2. Try updating both the task title and description.
    Test what happens if you only update one field and leave the other unchanged.

  3. Testing the Delete Function:
    Test deleting tasks by index, making sure the task is correctly removed from the list.
    Try providing invalid indexes (e.g., entering a number higher than the number of tasks) and ensure the error handling works as expected.

  4. Edge Cases:
    Test the program with no tasks to see if the proper error messages are shown when trying to view, update, or delete tasks.
    Enter incorrect values (like letters instead of numbers for the task index) and check how the program handles these inputs.

Output:

Image description

Conclusion

By the end of this tutorial, you have built a basic yet fully functional task manager using Python.

You learned how to:

  • Set up a development environment and work with basic Python input/output functions.

  • Implement key task management features such as adding, viewing, updating, and deleting tasks.

  • Test and debug your code to ensure that the task manager works smoothly under different conditions.

This project provides a great foundation for understanding how to handle user input and manage data in lists. You can further extend this project by adding features such as:

  1. Saving tasks to a file for persistence.
  2. Adding due dates to tasks.
  3. Creating categories for different types of tasks.

Acknowledgments

I was inspired to work on this task manager project which was provided by codingdidi after watching a helpful video on YouTube, which provided some guidance on how to structure the features. You can check out the video here for more insights: YouTube.

Top comments (0)