Bug Fix

The Confused Local

The Confused Local

Bug Report

A function tries to increment a global counter, but instead of updating it, Python throws an UnboundLocalError saying the variable is referenced before assignment.

Scopeglobal keywordUnboundLocalError
Fix the Bug: The Confused Local

If you assign to a variable ANYWHERE inside a function, Python treats it as a local variable for the ENTIRE function body — even on lines before the assignment. So trying to read a global variable and then reassign it in the same function without declaring 'global' first causes this error.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should declare 'global counter' inside the function
Should still increment counter by 1
Should keep the function name the same