NameError —
The name doesn't exist where you're looking for it.
You referenced a variable or function that Python can't find — either it was never created, it's misspelled, or it only exists inside a different scope than the one trying to use it. Watch the scope chain get checked on LearnBug and see exactly where the lookup fails.
What is NameError: name is not defined?
Python raises this when your code references a name — a variable or function — that isn't defined in any scope Python checks: local, then enclosing, then global, then built-in. If the name never appears as an assignment or definition somewhere Python would look, this is the result.
Why visualization helps
Scope is invisible in the source code — a variable defined inside a function looks textually identical to one defined outside it. LearnBug shows exactly which names exist in scope at each line of execution, so "this variable only exists inside that function" stops being something you have to reason about abstractly.
Three ways a name goes missing
Used outside the function it was defined in
A variable created inside a function only exists in that function's local scope — trying to read it from outside raises NameError, even right after calling the function.
Typo in the variable name
Python matches names exactly — total and totall are two completely different (unrelated) names as far as the interpreter is concerned.
Used before it was assigned
Referencing a variable earlier in the file (by line order, or execution order) than where it's actually assigned — Python runs top to bottom, so order matters.
message defined inside a function, used outside it
get_greeting() runs, creates a local variable message = "Hello!"
message is created inside the function's own local scope — it exists only for the duration of this call, and only accessible from code running inside it.
get_greeting() returns "Hello!" — the function's local scope is destroyed
Once the function returns, its local variables (including message) cease to exist entirely. The returned value "Hello!" is what survives, not the variable name that held it.
print(message) — Python checks local, then global scope. message exists in neither
At the module level (outside any function), there's no local scope for message to be found in, and it was never defined at the global level either — only inside the now-finished function call.
Result: NameError: name 'message' is not defined ✗
The broken code, and the traceback it produces
def get_greeting():
message = "Hello!"
return message
print(get_greeting())
print(message) # message doesn't exist out hereHello!
Traceback (most recent call last):
File "greeting.py", line 6, in <module>
print(message)
^^^^^^^
NameError: name 'message' is not definedUse the returned value, or store it where you need it
def get_greeting():
message = "Hello!"
return message
greeting = get_greeting() # capture the returned value in this scope
print(greeting)
print(greeting) # works — greeting exists at this levelQuick Reference
Frequently asked questions
What's the difference between NameError and UnboundLocalError?
NameError means the name isn't defined anywhere Python looked. UnboundLocalError is a more specific case — Python sees you assign to a name somewhere in the current function (which makes it local for the whole function), but you try to read it on a line that executes before that assignment happens. Both stem from the same underlying "name isn't available yet" issue.
What does "LEGB" mean?
It's the order Python searches for a name: Local (the current function), Enclosing (any outer function, for nested functions), Global (the module level), and Built-in (Python's own names like len or print). If a name isn't found in any of these four, NameError is raised.
Can I make a function's local variable accessible outside it?
The correct way is to return it and capture the result in the caller, as shown in the fix. The global keyword technically lets a function modify a module-level variable directly, but it's generally discouraged — returning values keeps functions predictable and testable.
Why does this error only show up when the code actually runs, not immediately?
Python is largely dynamic — it doesn't check that every name resolves correctly before running the program, only when each line actually executes. A NameError inside a function that's never called, or an if branch that's never taken, won't surface until that exact code path runs.
Watch the scope chain get checked, live
Paste your code into LearnBug and see exactly which names exist at every line.