๐Ÿ’ป Exercise: Exercise: The stopping condition

๐Ÿ“ Instructions

Write should_stop(text, step, max_steps) -- the terminal check that ends the agent loop. Return True when the loop should stop: either when step >= max_steps (a safety cap that guarantees the loop can never run forever), OR when the text contains the marker 'FINAL ANSWER:'. Otherwise return False. Standard library only.

๐Ÿงช Initial Code / Tests

๐Ÿ“„ evaluate.py
from unittest import TestCase
from exercise import should_stop


class Evaluate(TestCase):
    def test_stops_on_final_answer(self):
        self.assertTrue(should_stop('FINAL ANSWER: 7', 1, 6))

    def test_stops_at_step_cap(self):
        self.assertTrue(should_stop('still going', 6, 6))

    def test_continues_midway(self):
        self.assertFalse(should_stop('thinking', 2, 6))

    def test_step_cap_takes_priority_over_text(self):
        self.assertTrue(should_stop('no marker here', 10, 6))

โœ… Solutions

๐Ÿ“„ exercise.py
def should_stop(text, step, max_steps):
    if step >= max_steps:
        return True
    if 'FINAL ANSWER:' in text:
        return True
    return False