Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Python to-do list that saves tasks in a text file

Tasks are written to a file and read back, so the list is still there the next time the program runs. It teaches file handling and lists.

What it shows

Tasks are written to a file and read back, so the list is still there the next time the program runs. It teaches file handling and lists.

The science

Variables vanish when a program ends, but a file on disk stays. Opening a file with "a" adds to its end, "r" reads it and "w" replaces it. The with statement closes the file automatically. A list holds the tasks in memory while the program runs, and the program writes it back whenever it changes.

Materials

Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • A computer, laptop or phone with a browser (free)
  • Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
  • An empty folder to keep todo.py and the todo.txt file that the program will create
  • A notebook and a pencil

Safety

This project uses safe, everyday materials. Still, read the rules below with a parent before you start.

Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.

All the safety rules

Step by step

8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.

  1. Create todo.py and write a function load() that returns the tasks: try: with open("todo.txt") as f: return f.read().splitlines() and except FileNotFoundError: return [].

  2. Write save(tasks): with open("todo.txt", "w") as f: and f.write("\n".join(tasks)) indented below it.

  3. Start the main part with tasks = load() and while True: printing 1 Add, 2 Show, 3 Done, 4 Quit.

  4. For choice 1 read text with input and use tasks.append(text), then call save(tasks).

  5. For choice 2 use for number, task in enumerate(tasks, 1): and print(number, task).

  6. For choice 3 ask for the number with int(input()) and remove it with tasks.pop(number - 1), then save(tasks).

  7. For choice 4 use break; run the program, add three tasks, quit, and run again to check they are still there.

  8. Open todo.txt in Notepad to see your tasks stored as plain lines.

What you should see

You can add, view and finish tasks, and the list is still present after closing and reopening the program because it is stored in todo.txt.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

1Why save to a file?

Variables are lost when the program ends; a file keeps the data.

2What is the difference between "a" and "w"?

a adds to the end of a file; w erases it and writes new content.

3What does with open do?

It opens the file and closes it automatically afterwards.

4Why try and except?

The file does not exist on the first run, so the error must be handled.

Free ebook: Viva Questions for Science Projects

Take it further

Store a due date with each task and sort the list so the nearest date comes first.

Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.

Free ebooks for this project

WhatsApp us