Why does it say my variable is not defined?


#1

Question

I’ve written my code and when I press run it tells me that my variable isn’t defined; why is that?

Answer

It’s easy to overlook a typo in your code when you’re working through it the first time. If you run your code and are met with an error message saying something like below, then you likely misspelled a variable name and need to take a look back over the line number pointed out by the error message.

NameError: name ‘var_name’ is not defined

In the code below, for example, notice how the variable names are slightly different. It will give an error if we try to run it!

my_varaible = 10  # see the typo here?
print my_variable

FAQ: Learn Python - Python Syntax - Two Types of Division