Errors are a nightmare to every programmer. Most of the time, the problem is a typo, followed closely by a misunderstanding of indentation and standards.
A standard is how organized your code is. With python, unlike most languages, you define blocks of code like functions by indentation. Most python editors will automatically indent for you where it is necessary. With this, if you are ever coding along and find python automatically indenting you where you don't think it should, this should raise a flag for you to figure out.
There are some more in-depth common-issues that you'll find from time to time.For now I will just keep these ones basic.
The first error we'll discuss is the NameError
.
As obvious as this might appear to you, this gets people amazingly.
Just learn to recognize the is not defined
text associated with
``NameError``s. Chances are you typed the definition of the variable
incorrectly or when you referenced to it.
variable = 55
print(varaible)
The print
is correct but we have typoed the name variable
.
Another error that always get people is the indentation issues.
You will see expected an indented block
as a
popup when you never enter an indented block for
something that requires it, like a function.
def task1():
def task2():
print('more tasks')
The two functions are not separated as you can see. There should be an
indentation of four spaces between the two functions def task1():
and def task2():
.
With that code you will get the unexpected indent
error and you
can correct it using the following code.
def task1():
print('task')
def task2():
print('more tasks')
The following code also gives you an error as well.Indentation can be at times be very boring to correct.
def task():
print ('stuff')
print('more stuff')
print('stuff')
This usually happens when you do not close off the brackets and quotes.
def task():
print('some people find themselves committing this too
print('ouch...')
The best way to solve errors is to know the name of the error, as this makes it very easy to solve the error. The errors mentioned above are some of the most common errors that you will face in your programming journey. Every time you run your code, expect to get an error.