Explain Python scope errors
Paste a Python UnboundLocalError, such as "cannot access local variable 'total'", and Vibe explains it in plain English: why Python treats the name as local, which line made it local and what each fix changes.
Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Anthropic to write the explanation. Don't paste keys or passwords. Common key formats are refused.
Vibe9 lines selected
Press Explain it for the sample, or paste your own code first.
What this Python UnboundLocalError sample shows
- the message
UnboundLocalError: cannot access local variable 'total' where it is not associated with a valuemeans Python looked for atotalthat belongs toadd_saleitself and found it had no value yet. The^^^^^sits undertotalon line 7,total += sale["amount"], which line 14 called for the first sale. The run ended with exit code 1 before anything printed.- why it's local
total = 0at the top of the file is a different variable. Becauseadd_saleassigns tototal, and+=counts as assigning, Python treatstotalas local everywhere in that function, so+=tries to read the local one before it has a value. The error names the local variable, not the outer one it was meant to be.- reading is fine
- A function that only reads
totalworks. Withprint(f"{sale['id']}: running total so far {total}")in place of the+=, the script ran with no error and printedS1: running total so far 0and the same for S2 and S3, then3 sales, total 0.00. Assigning to the name inside the function is what makes it local. - global or return
- Adding
global totalas the first line ofadd_saleprinted3 sales, total 90.25with exit code 0. So did passing the total in and returning it,total = add_sale(total, sale), with noglobalat all. The second needs no variable from outside the function.
Good to know
- Run with Python 3.14.7 on Linux in a test folder mounted at /home/dev/sales, which is why the paths start with
/home/dev/sales. sales.json holds 3 sales, S1 to S3, of 29.5, 18 and 42.75. - Python 3.11.18 printed the same last line. The
~and^marks under each line point at the part of it that was running.
Before you trust it: Setting total = 0 inside the function makes the error go away and the answer wrong. With total = 0 as the first line of add_sale, the script printed 3 sales, total 0.00 with exit code 0: each call added to a new local total that was then thrown away, and the one at the top of the file stayed at 0. Check the number it prints, not only that the error is gone.
Explain Python scope errors beside your editor
Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).
Install on Mac
curl -fsSL https://unvibe.site/install.sh | bashPaste into Terminal. Unvibe isn't notarized by Apple yet, so this is the smooth path.
Or download the .dmg · Intel Mac build · SHA-256 checksums
No install warning? Use Unvibe in your browser
Opening the .dmg the first time
Unvibe isn't notarized by Apple yet, so macOS blocks the first open until you allow it. Its code is public on GitHub.
- Open the .dmg, drag Unvibe to Applications and open it. macOS says it can't verify it. Click Done, not the blue Move to Trash.

Real screen, macOS 26. On macOS 15 the two buttons sit side by side. - System Settings › Privacy & Security, scroll down to Security, then press Open Anyway. Confirm, then enter your Mac password. The button shows for about an hour after you try to open Unvibe.

Real screen, macOS 15 - Unvibe opens. You only do this once, and later versions install from inside the app with one click.

Real screen, Unvibe 0.1.51 on macOS 26
The Terminal line checks the download against the published SHA-256 and Unvibe's self-signed signature before it installs anything. More help with the first open