Capstone: Build a Command-Line Tracker
Build a practical task tracker by combining functions, lists, dictionaries, loops, input validation, and error handling.
In this lesson, you will learn
- ✓Explain how functions, lists, dictionaries, loops, and conditionals work together in a complete Python program.
- ✓Build a menu-driven command-line application that accepts and validates user input.
- ✓Add, display, complete, and delete tasks using a list of dictionaries.
- ✓Handle blank task names, invalid menu choices, non-numeric task IDs, and missing tasks without crashing.
- ✓Trace program execution, test edge cases, and explain the time and space complexity of important operations.
1. Plan the application before writing code
A command-line application accepts text from a terminal, performs an operation, and displays a result. Before coding, list the actions the user should be able to perform.
Our tracker will support five menu options: add a task, view all tasks, mark a task as completed, delete a task, and exit. A loop will keep showing the menu until the user chooses to exit.
We will represent each task as a dictionary and store all tasks in a list. A task dictionary will contain an integer ID, a title, and a Boolean completion status. For example, {'id': 1, 'title': 'Study Python', 'completed': False}.
The ID identifies a task independently of its current position in the list. This matters because deleting one task can change list positions, but the IDs of remaining tasks should not change.
We will divide the application into small functions. Each function will have one main responsibility, making the program easier to understand, test, and maintain.
Represent tasks with a list of dictionaries
The outer list stores multiple tasks, and each dictionary stores the properties of one task. The loop visits each dictionary in order. The conditional expression chooses 'Done' when completed is True and 'Pending' otherwise. The task ID, title, and completion status are accessed using dictionary keys.
Note
2. Build and understand each function
Start with the smallest useful operations rather than writing the entire program as one large block. The following functions handle task creation, displaying tasks, completing tasks, and deleting tasks.
The add_task() function validates the title and appends a dictionary. It receives the next available ID from its caller instead of relying on a global variable.
The list_tasks() function displays a helpful message if there are no tasks. Otherwise, it prints each task with a readable status.
The complete_task() and delete_task() functions search for a matching ID. They return True when the requested task exists and the operation succeeds, or False when the ID does not exist.
Notice that these functions receive the task list as an argument. They can modify the list because lists are mutable, while the function's return value communicates whether the operation succeeded.
Complete and delete tasks by ID
complete_task() searches for ID 2 and changes its completed value to True. delete_task() uses enumerate() to obtain both each list position and its dictionary. pop(index) removes the matching dictionary from the list. The return statement exits the function immediately after a successful operation. Deleting ID 1 does not change the ID of task 2.
Note
4. Complete project: Task Tracker
Now combine the functions into a single runnable program. This version uses only Python's standard features and requires no third-party packages.
The program supports adding tasks, displaying tasks, completing tasks, deleting tasks, and exiting. It rejects blank titles, handles invalid menu choices, and safely processes task IDs.
Copy the complete code into a file named task_tracker.py. Run it from a terminal with python task_tracker.py. On some systems, you may need to use python3 task_tracker.py.
Read the code in this order: data and helper functions, the menu, main(), and finally the if __name__ == '__main__' guard. Python defines the functions before main() calls them.
Complete runnable task tracker
The full code is the runnable program. The output is a representative session, not output produced without user input. main() creates an empty list and enters the menu loop. Each menu choice invokes a different function or exits the loop. Task dictionaries are modified in place when a task is completed or deleted. get_next_id() finds the largest currently stored ID and adds one. If the list is empty, max() uses zero as its default. The main guard ensures that main() runs when this file is executed directly, but not merely when it is imported as a module. The task list is held in memory only. Saving data between program runs is a possible future enhancement.
Note
5. Trace the program and test edge cases
A program is not complete just because its main path works. Test what happens when the list is empty, a title contains only spaces, a menu choice is unknown, an ID is not an integer, or a valid ID does not exist.
The task list changes over time. After adding a task, the list contains another dictionary. Completing a task changes a value in an existing dictionary. Deleting a task removes a dictionary from the list.
Notice that an invalid action should not accidentally change task data. For example, entering 'abc' for a task ID must not complete or delete anything.
You can test the functions independently without typing through the interactive menu. This is one reason small functions are useful: each function has inputs and observable behavior.
Test several important edge cases
The blank title is rejected and the list remains empty. The valid task is then added with ID 1. ID 99 does not exist, so completing or deleting it returns False. The next ID is 2 because the largest existing ID is 1. The task remains unchanged after the unsuccessful operations.
Caution
6. Understand time and space complexity
Complexity describes how the work and memory requirements of an algorithm grow as the amount of data grows. Let n represent the number of tasks.
Adding a task with append() takes amortized O(1) time for the list operation itself. However, this program's get_next_id() scans the existing IDs to find their maximum, so assigning the next ID takes O(n) time.
Displaying all tasks takes O(n) time because each task is visited once. Searching for an ID in complete_task() or delete_task() takes O(n) time in the worst case.
Deleting an item from the middle of a Python list may also shift later elements, which takes O(n) time. The complete delete operation therefore remains O(n) in the worst case.
The list stores n task dictionaries, so the task data uses O(n) space. Completing a task uses O(1) additional space, excluding the existing data. This analysis describes growth rates, not a promise of a particular number of milliseconds.
Count the work in a linear search
The first search checks ID 1 and then ID 2, returning the matching dictionary. The second search checks all three dictionaries before returning None. In the worst case, the function performs a number of comparisons proportional to n, so its time complexity is O(n). The function does not create another collection proportional to the number of tasks, so its auxiliary space complexity is O(1).
Note
Common pitfalls to avoid
Real debugging lessons and exact syntax contrasts to avoid runtime bugs
#1 Putting the entire application inside one large block of code.
Why it happens: Separating input handling, validation, data operations, and display into small functions makes the program easier to test and maintain.
Key takeaway: Separate adding, listing, completing, deleting, input parsing, and menu control into focused functions.
#2 Accepting a task title that contains only spaces.
Why it happens: Whitespace-only titles are not useful labels; normalize input with strip() and reject the result if it is empty.
Key takeaway: Call strip() before validating the title, then reject it if the result is empty.
#3 Passing the string returned by input() directly to code that expects an integer.
Why it happens: input() returns a string, so parse and validate a task ID before passing it to logic that expects an integer.
Key takeaway: Convert the input with int() inside a try/except block and handle ValueError.
#4 Assuming that every positive task ID exists.
Why it happens: A positive identifier may not correspond to a stored task; check whether the ID exists and report a useful message.
Key takeaway: After validating the ID's format and range, search for the ID and handle the not-found result.
#5 Using a task's list position as its permanent identity.
Why it happens: List positions can change when items are inserted or removed; use a stable task ID to identify a task over time.
Key takeaway: Store a separate ID in each task dictionary so deleting an item does not change the identity of other tasks.
#6 Assuming that an in-memory list automatically saves data.
Why it happens: An in-memory list is lost when the process exits unless the program explicitly writes data to persistent storage.
Key takeaway: Understand that the list disappears when the process ends. Add file or database persistence only when you are ready to extend the project.
#7 Claiming O(1) time for the entire add operation because list.append() is usually amortized O(1).
Why it happens: The append operation is usually amortized O(1), but the full add workflow may also validate input, generate IDs, and update other structures.
Key takeaway: Analyze all work performed by the operation. In this implementation, get_next_id() scans the list and makes adding a task O(n) overall.
Ready for hands-on challenges?
Test your skills with shuffled coding challenges in the Practice Arena.
Technical and placement interview questions
Frequently tested interview questions with detailed, full answers (click to expand)
Q1:What is the purpose of a capstone project?
Q2:Why did you use a list of dictionaries instead of separate lists?
Q3:Why should functions have separate responsibilities?
Q4:How do you handle invalid user input?
Q5:Why does the project assign IDs instead of relying on list indexes?
Q6:What is the time complexity of displaying all tasks?
Q7:What is the difference between time complexity and space complexity?
Q8:How would you preserve tasks after the program exits?
Q9:What does the if __name__ == '__main__' guard do?
Q10:How would you test this application?
Key takeaways
- A capstone project brings multiple programming concepts together into a working application.
- Lists store collections of tasks, while dictionaries represent individual tasks and their fields.
- Small functions make the application easier to understand, test, and maintain.
- Input validation and exception handling prevent common mistakes from crashing the program.
- Stable task IDs avoid relying on list positions that can change after deletion.
- The menu loop continues until the user chooses to exit.
- The task list is stored in memory; persistent storage requires an additional file or database feature.
- Searching and displaying tasks take O(n) time in the worst case, while the task collection uses O(n) space.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: