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
- Add
import json(next toimport pip). - Store
pip.ask("Give me your mood as JSON")inreply. - Turn it into a dict with
json.loads(reply)and store it indata. - Print
data["mood"].
💡 Hint
data = json.loads(reply) makes a dict. Then data["mood"] reads one value out of it.
bot.py
Terminal