I spelled everything correctly, but still receive a NameError. Why?

Question

Within a Python program, everything is spelled correctly, but I still receive a NameError. Why?

Answer

There are some possible reasons that a NameError can appear even if everything is spelled correctly in the code.

Reason 1: Inconsistent casing

One common reason is due to inconsistent casing of any variable names or functions. For example, Python will view Name and name as two completely different variables, despite them having the same spelling because the casing of each letter is not the same.

Code example

name = "Monty"
print(Name)
# This will output a NameError because 
# the casing is not consistent.

Print("Hi!")
# This will output a NameError because 'Print' is not defined.

Reason 2: Ordering of the code

Another common reason is due to code ordering. The variable was used on a code line earlier than the code line where it was defined. Python generally processes the code from top to bottom, so if it encounters a variable not yet defined, it would throw a NameError.

Code example

print(year)
year = "2018"
# A NameError will be thrown because year is not yet defined 
# when the first line of code runs.
21 Likes

Hello
The example presented during the lecture didn’t throw any NameError before SyntaxError has been cleared.
I would like to know why it doesn’t display both SyntaxError and NameErrors at the same time.
please reply.
Thank you.

10 Likes

Syntax is checked in the parsing stage, pre-compile, so those errors are raised before the code is even run. NameError is a runtime exception that will only be encountered when the code is being executed, post-compile.

40 Likes

Hello Roy
Your face reminds me an old friend in virginia.
Thank u for all those comments

4 Likes

Hi friend.

Regarding your response there are some concepts I m lost. Can you please explain in simple words the following:

  • parsing :
  • pre-compile :
  • post-compile :
  • runtime exception :

Regards.

1 Like

Parsing is the analyzing of text for keywords, arguments, data, &c. It is the interpretive phase.

Pre-compile is when the parsed code is translated into byte code. I believe in Python this is the form it takes before it is given over to the compiler to put into machine language.

Post-compile is the executable machine code.

Runtime exception is any error that occurs during runtime. It is usually data related but could be due to errant logic or some other issue such as out of memory, etc. These are fatal errors that cease program operation in its tracks.

17 Likes

The variable name was spelled correctly but was used before any data was assigned to it.

1 Like

Great to know @mtf Thanks a lot!
Your sharing is the base knowledge. But, I bet many people doesn’t know these, including me. You did show me code academy is great place to learn about coding.

2 Likes

How can you avoid NameError in your code and ensure that all variables and functions are defined and declared in the right order?

As said earlier by someone, Python well execute the code from top to bottom, if you initialize a variable and try to use that variable in an expression or other means, before the creation of the variable, Python will get confused, and throw a NameError.

To your question, everyone has their own way of where or when they initialize the variables, if you know beforehand, the data a variable will hold you can place them at the top (grouping them together, this is called global variables, and can be accessed outside or inside a function) or initialize the variables when you need them, this will however scatter the variables instead of keeping them in one place, for example at the top, it’s all personal preferences.
But it’s important to keep your variables organized, by giving them accurate names corresponding to the data they contain, when you projects gets longer and longer, you can easily lose track of your variables if they are named badly, so try to keep than in mind.

For functions, the same principles applies, you can create function when you need them, or place them at the top, but always keep them organized, also if you create a function and invoke/call the func before the creation you will also get a NameError.
This becomes more important once you delve in OOP(Object-Oriented Programming).

Question: I spelled everything correctly, but still received a NameError. Why!
Answer:
A nameError presents when the name is spelled differently at two different locations in a program
or
Misspelled built-in functions.

Ex.
geek = input()
Print(geek)

See: What causes a NameError in Python - Search (bing.com)