Why This Happens
Python raises NameError when you reference a name that does not exist in the current scope. Common causes include typos, using a variable before assignment, and forgetting imports. Python is case-sensitive.
The Problem
for i in range(10):
if i > 5:
result = i
print(result)The Fix
result = None
for i in range(10):
if i > 5:
result = i
print(result)Step-by-Step Fix
- 1
Check for typos
Python is case-sensitive: 'Result' and 'result' differ.
- 2
Initialize variables
Define variables before conditional blocks.
- 3
Check scope
Variables inside functions are not accessible outside.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free