Why does it say my variable is not defined?

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
2 Likes

I defined a variable with variable = x/y but it says not defined. Equasions not be used for a definition?

Can we see your full code? Did you defined x and y before using these variables?

I don´t have the code, but I defined them with numbers. Maybe it was just a typo

them? You mean the x and y variable? So you have more code then you posted on the forum?

oh, no. We can’t use x and y like in a math equation. Now x and y are undefined variables.