Challenge: Diary File
Completed
Read n diary entries and write them to a file called diary.txt, one per line. Then open the file again, read it back, and print each line numbered, followed by the total.
Input
A number n, then n lines of text.
Output
Lines like 1: Learned loops, then Entries: <n>.
Example
Input
3 Learned loops Wrote a function Fixed a bug
Output
1: Learned loops 2: Wrote a function 3: Fixed a bug Entries: 3
Editor
Python
Hints
Open with "w" to write and add "\n" after each entry.
Open again (no mode) to read it line by line.
line.strip() removes the new-line at the end.
Solution explanation
Writing then reading the same file shows the data really was saved. The checker only sees what you print, so the second part must read from the file.
Show solution
n = int(input())
with open("diary.txt", "w") as file:
for _ in range(n):
file.write(input() + "\n")
count = 0
with open("diary.txt") as file:
for line in file:
count += 1
print(f"{count}: {line.strip()}")
print("Entries:", count)