Make retry delays predictable and bounded
Retries need a policy rather than a sleep scattered through the code. A pure function can produce the planned delays without actually waiting or sending requests. Exponential backoff increases the gap after each failure, and a cap prevents unbounded pauses. Random jitter can be added by the caller, using an injected random source when reproducible tests matter.
delay = 0.25
for attempt in range(3):
print(delay)
delay = min(1.0, delay * 2)Think before coding
A plan is easy to test because it has no clock or network effects. Iterative doubling avoids computing enormous powers after the cap has already been reached. Actual retrying still belongs at the effect boundary. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write retry_delays(attempts, base, cap). Return attempts delays, starting at base and doubling up to cap. attempts must be nonnegative and base and cap must be positive; otherwise raise ValueError. The first delay is also capped.
The explanation opens here once every check passes.