Chapter 4 · Quest 2 of 5 · 10 XP
Diary
📔 A dict disappears when the program stops. A file doesn't. Bots keep diaries in files so they remember yesterday.
with open("log.txt", "a") as f:
print("Woke up", file=f)
with open("log.txt") as f:
print(f.read())Woke up
"a" means append: add to the end without wiping what's there. print(..., file=f) writes one line into the file instead of the terminal. Each Run starts with an empty folder, so your diary starts fresh every time.
Instructions
- Make
write(entry)that appendsentryas one line todiary.txt. - Make
read_diary()that returns a list of the lines indiary.txt. - Write the 3 entries in
days, then print how many linesread_diary()gives back.
💡 Hint
In read_diary, open the file and return f.read().splitlines(). Then print(len(read_diary())).
bot.py
Terminal