
Project
Capstone 3: Speaking Commands
Keepers will type commands like:
add "Fix the bridge" Ember 1
done 3
list Frost
report
str.split() would break "Fix the bridge" into three words. The
standard library's shlex.split splits the way a shell does, so
quoted text stays together (and an unclosed quote raises ValueError):
shlex.split('add "Fix the bridge" Ember')
# ['add', 'Fix the bridge', 'Ember']
match with sequence patterns is the perfect tool for checking the
word list. Each pattern checks both the shape and the values:
match words:
case ["done", task_id]: # exactly 2 words
...
case ["list"] | ["list", _]: # 1 or 2 words
...
case [name, *rest]: # anything else
...
Assignment
Write parse_command(line) that returns a dict:
| Input | Result |
|---|---|
add <title> <island> |
{"cmd": "add", "title": ..., "island": ..., "priority": 2} |
add <title> <island> <p> |
same, with "priority": int(p) |
done <id> |
{"cmd": "done", "id": int(id)} |
list / list <island> |
{"cmd": "list", "island": None or island} |
report |
{"cmd": "report"} |
Raise ValueError for everything else. Use the message
"unknown command: <word>" when the first word isn't a known command.
Write parse_command(line) that returns a dict:
| Input | Result |
|---|---|
add <title> <island> |
{"cmd": "add", "title": ..., "island": ..., "priority": 2} |
add <title> <island> <p> |
same, with "priority": int(p) |
done <id> |
{"cmd": "done", "id": int(id)} |
list / list <island> |
{"cmd": "list", "island": None or island} |
report |
{"cmd": "report"} |
Raise ValueError for everything else. Use the message
"unknown command: <word>" when the first word isn't a known command.
main.py Python sleeping
Press Run to try your code, then Check to test it.