Kids Coding
Project: To-Do
Add, list, and remove tasks. A list in memory, then save it to a file.
A list you can change
A to-do app is a list plus a few actions: show, add, remove. Here the actions run in order so the editor can finish.
Add, list, remove
tasks = []
def show(tasks):
if not tasks:
print("(empty)")
return
for i, task in enumerate(tasks, start=1):
print(i, task)
tasks.append("Feed the fox")
tasks.append("Finish homework")
print("After add:")
show(tasks)
tasks.pop(0)
print("After remove first:")
show(tasks)
with open("tasks.txt", "w", encoding="utf-8") as file:
for task in tasks:
file.write(task + "\n")
print("Saved")Save the list to a file so the tasks are still there tomorrow. A to-do app that forgets everything when it closes is not much of a helper.
On a computer
Wrap show, append, and pop in a while True menu that inputs list, add, done, or quit. Same functions. Different driver.
Real life
Saturday chores on the fridge, then crossing one off.
A list you add to and take from. That is a to-do in real life.
Saturday list
chores = ["Wash cups", "Walk the dog"]
chores.append("Tidy desk")
print("To do:")
for chore in chores:
print("-", chore)
chores.pop(0)
print("After one done:", chores)Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Start with tasks = ["a"]. Append "b". Print the list.
Show solution
tasks = ["a"]
tasks.append("b")
print(tasks)