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")

```
```
for _ in range(5):
    try:
        # replace this with something that may fail
        raise ValueError("foo")

    # replace Exception with a more specific exception
    except Exception as e:
        err = e
        continue

    # no exception, continue remainder of code
    else:
        break

# did not break the for loop, therefore all attempts
# raised an exception
else:
    raise err

```

My version is similar to several of the above, but doesn't use a separate `while` loop, and re-raises the latest exception if all retries fail. Could explicitly set `err = None` at the top, but not strictly necessary as it should only execute the final `else` block if there was an error and therefore `err` is set.