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

Stop Sign

🛑 Some goals never finish. A bot that keeps trying forever burns battery (and real money, with real models). Every agent needs a step limit. break jumps out of a loop early, and return inside a loop leaves the whole function.

for n in range(100):
    if n == 3:
        break
    print(n)
0
1
2

pip.next_step(goal, done) tells your agent what to do next, or "DONE" when the goal is finished. For a goal Pip can't finish, it just says "thinking..." forever.

Instructions

  1. Make run_agent(goal, limit) with an empty list done inside.
  2. Loop at most limit times. Each time, ask pip.next_step(goal, done): if it says "DONE", return Finished in 3 steps. (with the real count).
  3. Otherwise add the step to done. If the loop runs out, return Stopped after 5 steps. (with the limit).
  4. Print run_agent("count to 3", 5) and then run_agent("count to infinity", 5).
💡 Hint

for i in range(limit): step = pip.next_step(goal, done). If step == "DONE": return "Finished in " + str(len(done)) + " steps." After the loop, return the Stopped message.

Ask the community

bot.py
Terminal
← BackNext →

Chapter 5: Loops and Plans

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.