NameError: Name is Not Defined

NameError: name 'result' is not defined

Quick Answer

You are using a variable that has not been defined. Check for typos, make sure it is defined before use, and verify scope.

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. 1

    Check for typos

    Python is case-sensitive: 'Result' and 'result' differ.

  2. 2

    Initialize variables

    Define variables before conditional blocks.

  3. 3

    Check scope

    Variables inside functions are not accessible outside.

Bugsly 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