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.

unbound-local.txt

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 value means Python looked for a total that belongs to add_sale itself and found it had no value yet. The ^^^^^ sits under total on 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 = 0 at the top of the file is a different variable. Because add_sale assigns to total, and += counts as assigning, Python treats total as 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 total works. With print(f"{sale['id']}: running total so far {total}") in place of the +=, the script ran with no error and printed S1: running total so far 0 and the same for S2 and S3, then 3 sales, total 0.00. Assigning to the name inside the function is what makes it local.
global or return
Adding global total as the first line of add_sale printed 3 sales, total 90.25 with exit code 0. So did passing the total in and returning it, total = add_sale(total, sale), with no global at 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 | bash

Paste into Terminal. Unvibe isn't notarized by Apple yet, so this is the smooth path.

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.

  1. 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.
    macOS dialog: “Unvibe” Not Opened, with a blue Move to Trash button on top and Done under it
    Real screen, macOS 26. On macOS 15 the two buttons sit side by side.
  2. 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.
    macOS Privacy & Security, Security section: Unvibe was blocked to protect your Mac, with the Open Anyway button
    Real screen, macOS 15
  3. Unvibe opens. You only do this once, and later versions install from inside the app with one click.
    Unvibe's first window: Vibe says Hey! I'm Vibe. I explain the code AI writes for you, right beside your editor, over the title Understand the code AI writes
    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