pythonintermediate15 minutes

Build a Task Manager CLI Application in Python

Create a command-line task manager in Python that allows users to add, complete, delete, and list tasks with due dates and priorities.

Challenge prompt

Write a Python program that manages a list of tasks. Each task should have a description, a due date (YYYY-MM-DD format), a priority level (low, medium, high), and a completion status. Implement functionality to: 1. Add a new task. 2. Mark a task as completed. 3. Delete a task. 4. List all tasks sorted by due date and then by priority (high to low). The program should interact with the user through simple text input commands and print the updated task list after each operation.

Guidance

  • Use a list of dictionaries or custom objects to store tasks.
  • Parse date strings into Python date objects for accurate sorting.
  • Define functions for each command (add, complete, delete, list) to keep the code organized.

Hints

  • Use the datetime module to handle and compare due dates.
  • Map priority levels to numeric values (e.g., high=3, medium=2, low=1) to facilitate sorting.
  • Keep track of task indices so users can reference them when completing or deleting.

Starter code

from datetime import datetime

tasks = []

def add_task(description, due_date_str, priority):
    due_date = datetime.strptime(due_date_str, '%Y-%m-%d')
    priorities = {'low': 1, 'medium': 2, 'high': 3}
    task = {
        'description': description,
        'due_date': due_date,
        'priority': priorities.get(priority.lower(), 1),
        'completed': False
    }
    tasks.append(task)

def list_tasks():
    sorted_tasks = sorted(tasks, key=lambda x: (x['due_date'], -x['priority']))
    for i, task in enumerate(sorted_tasks):
        status = 'Done' if task['completed'] else 'Pending'
        print(f'{i}. {task["description"]} - Due: {task["due_date"].date()} - Priority: {task["priority"]} - Status: {status}')

# Implement functions complete_task(index) and delete_task(index) and input loop here

# Example usage:
# add_task('Complete Dev Duel challenge', '2024-07-15', 'High')
# list_tasks()

Expected output

0. Complete Dev Duel challenge - Due: 2024-07-15 - Priority: 3 - Status: Pending

Core concepts

datetime handlinglist sortingdictionary usagecommand-line interaction

Challenge a Friend

Send this duel to someone else and see if they can solve it.