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

Science project idea

Python daily diary that adds dated entries to a file

The program appends each entry with today’s date to a file and can print the whole diary. It teaches the date module and the append and read modes of files.

What it shows

The program appends each entry with today’s date to a file and can print the whole diary. It teaches the date module and the append and read modes of files.

The science

The datetime module gives today’s date from the computer’s clock. Opening a file with mode "a" adds text at the end without erasing old lines, so the diary grows. Opening with "r" reads everything back. Catching FileNotFoundError lets the program say the diary is empty instead of crashing.

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)
  • 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 diary.py and write from datetime import date on the first line.

  2. Define def add_entry(): with text = input("Write today’s entry: ") as the first line (use a plain quote if you prefer).

  3. Next line inside it: with open("diary.txt", "a") as f: and below that f.write(str(date.today()) + " : " + text + "\n").

  4. Define def show_diary(): and use try: with open("diary.txt") as f: print(f.read()).

  5. Add except FileNotFoundError: print("The diary is empty").

  6. Write the main loop: while True: with a choice of 1 Write, 2 Read, 3 Quit and if and elif calling the functions.

  7. Run it, write three entries, choose Read and open diary.txt in Notepad to compare.

  8. Add a search option that prints only lines containing a word typed by the user, using if word in line.

What you should see

Each entry is saved on its own line with the date, the diary is shown when asked, and old entries remain when the program is run again.

Viva questions

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

1What does mode "a" mean?

Append, which adds to the end of the file.

2Why str(date.today())?

A date is not text, so it must be converted before joining with text.

3What is \n in a string?

A new line, so each entry starts on its own line.

4What if the file is missing?

The except block prints a message instead of crashing.

Free ebook: Viva Questions for Science Projects

Take it further

Add a password check at the start using a stored word so that only you can read the diary.

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