retry(func, attempts) that calls the zero-argument func() up to attempts times, returning its result on the first success. If every attempt raises, re-raise the last exception. No sleeping needed — this is about the control flow.retry(flaky, 3) # fails twice then returns 'ok'
'ok'
Third attempt succeeds; earlier exceptions are swallowed.
Save the exception in a variable in the except block.
A bare raise inside except also works on the last iteration.