livepython.net

173. Python Program to Append Entries to a Diary File

MediumWrite

Overview

Mode "w" starts a file fresh. Mode "a" adds to its end and keeps what's there.

Problem Statement

Read a whole number n, then n diary entries, one per line. Your program should:

  1. Open diary.txt with mode "w" and write the title line My Diary.
  2. For each entry, open diary.txt with mode "a" and add the line Day 1: (then Day 2: , and so on) followed by the entry.
  3. Read the whole file and print it.

If n is 0, the diary has only its title.

Your program runs in the browser, so the file lives only in this tab's memory. Create it with "w" before you read it.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Input

The number of entries, then the entries. Example:

2
Learned lists
Learned loops

Output

The output for that example:

My Diary
Day 1: Learned lists
Day 2: Learned loops

The tests try 4 different inputs, including 0 entries.

Learn this firstChapter 19 · Python Write to File: write() and Append Mode
solution.py
# write your code here

EXPECTED OUTPUT

1My Diary
2Day 1: Learned lists
3Day 2: Learned loops

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.