0%
⭐ 0 XPMap
Chapter 3 · Quest 5 of 5 · 15 XP

Toolbelt

🧰 Not every question needs a tool. Ask Pip for a joke and it answers by itself: "tool" comes back as null, which json.loads() turns into Python's None, and the answer is in "reply".

{"tool": null, "reply": "Why did the robot go on holiday? ..."}

A good agent handles both. Test for nothing with is None:

if choice["tool"] is None:
    print("No tool needed")

Build the toolbelt and finish the chapter for your Handy badge: your bot gets arms.

Instructions

  1. Make use_tools(question). It asks pip.ask_with_tools(question, tools) and loads the JSON.
  2. If the tool is None, return the "reply". Otherwise run the tool and return its result.
  3. Loop over questions and print use_tools(q) for each one.
💡 Hint

Inside use_tools: choice = json.loads(...). Then if choice["tool"] is None: return choice["reply"]. After that, return tools[choice["tool"]](**choice["args"]).

Ask the community

bot.py
Terminal
← BackNext →

Chapter 3: Tools

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.