0%
⭐ 0 XPMap
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

  1. Make write(entry) that appends entry as one line to diary.txt.
  2. Make read_diary() that returns a list of the lines in diary.txt.
  3. Write the 3 entries in days, then print how many lines read_diary() gives back.
💡 Hint

In read_diary, open the file and return f.read().splitlines(). Then print(len(read_diary())).

Ask the community

bot.py
Terminal
← BackNext →

Chapter 4: Memory

The full quest map

Talk it out

  1. Read the instruction with the ✗ out loud.
  2. Say what your code does, line by line, as if to a friend.
  3. Find the line where what it does and what you want split apart.
  4. Press Run and read the terminal. Is that what you expected?
  5. Still stuck? Press Ask Pip why, or peek at the solution.
  6. Or ask on the community help board: say which quest, and paste your code.
New badge!

Your bot just got a new part. See it on the quest map.

Name your bot

It levels up as you play, and gets a new part with every chapter.