`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.
More on Runtime, file, subprocess & encoding errors
`KeyError: 'Tags'` while processing `describe_instances` output.Beginner`TypeError: can't compare offset-naive and offset-aware datetimes` in a snapshot-age check.Beginner`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte` reading a log file.Intermediate`UnicodeEncodeError: 'ascii' codec can't encode character '✓'` when printing in a container.Intermediate`PermissionError: [Errno 13] Permission denied: '/var/log/app/report.json'`Intermediate`OSError: [Errno 24] Too many open files` in a long-running Python service.Intermediate