.. _undefined-loop-variable:

undefined-loop-variable / W0631
===============================

**Message emitted:**

Using possibly undefined loop variable %r

**Description:**

*Used when a loop variable (i.e. defined by a for loop or a list comprehension or a generator expression) is used outside the loop.*

**Problematic code:**

.. literalinclude:: /data/messages/u/undefined-loop-variable/bad.py
   :language: python

**Correct code:**

.. literalinclude:: /data/messages/u/undefined-loop-variable/good.py
   :language: python



Created by the `variables <https://github.com/PyCQA/pylint/blob/main/pylint/checkers/variables.py>`__ checker.