0%
⭐ 0 XPMap
Chapter 2 · Quest 4 of 5 · 10 XP

Tidy Reply

🧹 Bots often need answers they can read, not just chat. So we ask the model for JSON: text written like a Python dictionary. json.loads() turns it into a real dict.

import json
text = '{"hp": 10, "class": "mage"}'
hero = json.loads(text)
print(hero["class"])
mage

Ask Pip for JSON and it answers with its mood and battery.

Instructions

  1. Add import json (next to import pip).
  2. Store pip.ask("Give me your mood as JSON") in reply.
  3. Turn it into a dict with json.loads(reply) and store it in data.
  4. Print data["mood"].
💡 Hint

data = json.loads(reply) makes a dict. Then data["mood"] reads one value out of it.

Ask the community

bot.py
Terminal
← BackNext →

Chapter 2: Talk to the Model

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.