Day 14 90 min beginner

Project: CLI Task Manager

Put your knowledge of functions, logic, and files into practice by building a real application

Learning Objectives

  • Organize code into functional blocks
  • Implement a persistent storage system using text files
  • Handle user input and application flow
  • Practice basic error handling

Week 2 Milestone

Congratulations on finishing Week 2! You’ve learned about functions, scope, errors, and files. Now it’s time to bring it all together by building a Command Line Task Manager.

Project Requirements

Your application should allow a user to:

  1. View all tasks.
  2. Add a new task.
  3. Delete a task.
  4. Save tasks to a file (tasks.txt) so they are still there when the program restarts.

Step 1: Planning the Functions

Break the problem down into smaller tasks:

  • load_tasks(): Read from the file and return a list.
  • save_tasks(tasks): Write the current list to the file.
  • show_menu(): Print the user options.
  • main(): The loop that runs the app.

Step 2: Implementation Starter

task_manager.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def load_tasks():
    try:
        with open("tasks.txt", "r") as f:
            return [line.strip() for line in f.readlines()]
    except FileNotFoundError:
        return []

def save_tasks(tasks):
    with open("tasks.txt", "w") as f:
        for task in tasks:
            f.write(task + "\n")

def main():
    tasks = load_tasks()
    
    while True:
        print("\n--- TASK MANAGER ---")
        print("1. View Tasks")
        print("2. Add Task")
        print("3. Delete Task")
        print("4. Exit")
        
        choice = input("Choose an option: ")
        
        if choice == "1":
            for i, t in enumerate(tasks):
                print(f"{i+1}. {t}")
        elif choice == "2":
            new_task = input("Enter task: ")
            tasks.append(new_task)
            save_tasks(tasks)
        elif choice == "4":
            break
        else:
            print("Invalid choice!")

if __name__ == "__main__":
    main()

Your Challenge

Enhance the project with these features:

  • Delete Functionality: Ask for a task number and remove it from the list.
  • Error Handling: Use try...except to handle cases where the user enters a non-number for deletion.
  • Clear UI: Add some spacing or dividers to make the output look nice.

Interactive Practice

How would you visualize the loop of this application?

flowchart TD Start([Start]) --> Load[Load tasks.txt] Load --> Menu[Display Menu] Menu --> Input[Get User Choice] Input --> Action{Choice?} Action -- 1 --> View[Print List] Action -- 2 --> Add[Add to list & Save] Action -- 3 --> Del[Remove & Save] Action -- 4 --> Exit([End]) View --> Menu Add --> Menu Del --> Menu

Quiz

Complete this quiz with a minimum score of 80% to mark Day 14 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!