Previous Index Next

Types of Errors

As you do programming, you will naturally encounter a lot of errors. Here are three types of Python errors.

Syntax errors

Syntax errors are the most basic type of error. They arise when the Python parser is unable to understand a line of code. Syntax errors are almost always fatal, i.e. there is almost never a way to successfully execute a piece of code containing syntax errors.

In IDLE, it will highlight where the syntax error is. Most syntax errors are typos, incorrect indentation, or incorrect arguments. If you get this error, try looking at your code for any of these.

Example:

print "Hello World!"

In this first example, we forget to use the parenthesis that are required by print(). Python does not understand what you are trying to do.

Logical errors

These are the most difficult type of error to find, because they will give unpredictable results and may crash your program. A lot of different things can happen if you have a logic error.

Example: For example, perhaps you want a program to calculate the average of two numbers and get the result like this :

x = 3
y = 4
average = x + y / 2
print(average)

Output

5.0

Run time errors

Run time errors arise when the python knows what to do with a piece of code but is unable to perform the action.Since Python is an interpreted language, these errors will not occur until the flow of control in your program reaches the line with the problem. Common example of runtime errors are using an undefined variable or mistyped the variable name.

day = "Sunday"
print(Day)

Output

Traceback (most recent call last):
  File "C:/Users/91981/AppData/Local/Programs/Python/Python38/hello.py", line 2, in 
    print(Day)
NameError: name 'Day' is not defined

Previous Index Next