Question: I have a loop starting with `for i in range(0, 100)`. Normally it runs correctly, but sometimes it fails due to network conditions. Currently I have it set so that on failure, it will `continue` in the except clause (continue on to the next number for `i`).

Is it possible for me to reassign the same number to `i` and run through the failed iteration of the loop again?

Answer: 
**UPDATE 2021-12-01**:

As of June 2016, the retrying package is no longer being maintained.
Consider using the active fork [github.com/jd/tenacity](https://github.com/jd/tenacity), or alternatively [github.com/litl/backoff](https://github.com/litl/backoff).

---

The [retrying package](https://pypi.python.org/pypi/retrying) is a nice way to retry a block of code on failure.

For example:

```
@retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
    print("Randomly wait 1 to 2 seconds between retries")

```
A generic solution with a timeout:

```
import time

def onerror_retry(exception, callback, timeout=2, timedelta=.1):
    end_time = time.time() + timeout
    while True:
        try:
            yield callback()
            break
        except exception:
            if time.time() > end_time:
                raise
            elif timedelta > 0:
                time.sleep(timedelta)

```

Usage:

```
for retry in onerror_retry(SomeSpecificException, do_stuff):
    retry()

```