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:
- Open
diary.txtwith mode"w"and write the title lineMy Diary. - For each entry, open
diary.txtwith mode"a"and add the lineDay 1:(thenDay 2:, and so on) followed by the entry. - 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.