Interview Prep/Python for DevOps/Runtime, file, subprocess & encoding errors

`UnboundLocalError: cannot access local variable 'count' where it is not associated with a value`

BeginnerTroubleshootingRuntime, file, subprocess & encoding errors
Answer

Likely causes → The function assigns to count, which makes it local everywhere in the function, but reads it before the assignment (intending the global); or the variable is only assigned inside an if/try branch that did not run.

Diagnose → Find all assignments to the name inside the function.

Fix → Initialize the variable before branches, pass it in as a parameter and return the new value, or use a class/closure. Avoid global.

Prevent → ruff and mypy flag possibly-undefined variables; keep functions free of hidden global state.

All Python for DevOps questions Roadmap: Python for automation

More on Runtime, file, subprocess & encoding errors