Syllabus Lesson 201 of 239 · Projects: Build Real Things
Projects: Build Real Things

Project: a Task CLI

Time to build a whole program, not fill in a blank. You will write a command-line task manager: the user types things like add Buy milk, done 1, list, and the program responds. We will not use input() (it does not exist in this sandbox). Instead your program is a single function run(commands) that takes a list of command strings and returns a list of response lines, one per command. That design is also exactly how you test a CLI: feed it a script, check the transcript.

The shape of it:

def run(commands):
    state = {"tasks": []}      # this is your whole program's memory
    out = []
    for raw in commands:
        # look at the first word, decide what to do, append a response
        ...
    return out

You hold all the program's memory in one state dict so it survives across commands within a run. Each task is a little dict like {"text": "Buy milk", "done": False}.

The commands (the first word is the verb, the rest is the argument):

  • add <text> -> append a task, respond added: <text>
  • done <n> -> mark task number n done (1-based), respond done: <text>, or no task <n> if it does not exist
  • rm <n> -> remove task n, respond removed: <text>, or no task <n>
  • list -> one line per task as 1. [x] Buy milk (done) or 2. [ ] Walk dog (todo); on an empty manager respond (empty)
  • clear -> drop every task, respond cleared
  • anything else -> respond unknown command: <cmd>

Tip: cmd.split(maxsplit=1) gives you [verb, rest] in one shot. Watch the edge cases the tests care about: listing when empty, an unknown verb, and done on a task that is already done (it should stay done, not toggle). Press Run to watch a whole session play out line by line.

Your turn

Write run(commands) that drives a task manager over a list of command strings and returns the response transcript. Support add <text>, done <n> (1-based), rm <n>, list (rendering 1. [x] text / 2. [ ] text, or (empty)), clear, and an unknown command: <cmd> fallback. Keep all state in one dict so it persists across the run.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output