Exercises
7.1 — Write a program, save_load.py, that opens a file in append mode and
adds a timestamped line to the file. When the program starts, it reads the contents
of the file and prints them. Then, it repetitively prompts the user to enter a line of
text, writes the line and a timestamp to the file and asks the user if they want to continue.
7.2 — Write a program, count_lines.py, that takes a filename as a command line argument,
and prints the number of lines, words, and characters (similar to Unix utility wc).
7.3 — Write a program, todo.py, a task management utility that remembers your tasks
between runs by saving them to a file.
Spec.
- Tasks are stored in
tasks.jsonas a list of tuples, each like("buy milk", false). - On start, load the tasks (empty list if the file doesn't exist)
- Support these commands (via a menu loop):
add <text>— add a new task (not done).list— show all tasks, numbered, with[x]/[ ]for done/not-done.done <n>— mark tasknas done.remove <n>— delete taskn.quit— exit.
- Save to
tasks.jsonafter any change, so the list persists. - Decompose into functions:
load_tasks,save_tasks,add_task,mark_done,remove_task,show_tasks, and amain()that coordinates everything.