In Python, various types of errors can occur during program execution. These errors are known as exceptions and are raised when certain conditions are not met or when unexpected situations arise. In this topic, we will explore some commonly encountered exceptions in Python, including ImportError, EOFError, NameError, and IndexError. Understanding these exceptions and their causes is crucial for writing robust and error-free Python code.
ImportError
The ImportError may occur if you import a non-existing function:
from math import square
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ImportError: cannot import name 'square'
Or when a spelling mistake was made in the module name:
import maths
# Traceback (most recent call last):
# File "main.py", line 1, in <module>
# import maths
# ModuleNotFoundError: No module named 'maths'
Note that in this case, the ModuleNotFoundError is a subclass of the ImportError. Why so? The module math exists in the first example, but there's no such function as square. In Python, there's no special error subclass for this situation, so a more general ImportError is raised. In the second example, however, we try to import the module that doesn't exist in Python, so the ModuleNotFoundError is raised.
Apart from checking the spelling, make sure that the module you want to import is installed. If you forgot to do so, it would not be available in your program, so Python will raise this error.
EOFError
Now let's discuss the EOFError which is raised when the input has no data to read. You can run into this error on the platform when, for instance, you have 2 integers as an input, one per line, but you call input() three times:
first = input()
second = input()
third = input() # this was not expected
Then, the output of the tests can look like this:
Failed test #1 of 5. Runtime error
Error:
Traceback (most recent call last):
File "jailed_code", line 2, in <module>
third = input()
EOFError: EOF when reading a line
You will not come across this problem often outside Hyperskill. When you get this error on Hyperskill, make sure that you read the input exactly as many times as it is stated in the task description.
NameError
Take a look at the following code:
prant('Hello, world!')
# Traceback (most recent call last):
# File "main.py", line 1, in <module>
# prant('Hello, world!')
# NameError: name 'prant' is not defined
The function print() is misspelled, so Python does not recognize it. The situation is the same when you mess up the variable names:
a = 'Hello, world!'
print(b)
# Traceback (most recent call last):
# File "main.py", line 2, in <module>
# print(b)
# NameError: name 'b' is not defined
If you get this error, just make sure that all functions and variables are correctly spelled and refer to the existing objects.
IndexError
Finally, let's proceed to the IndexError.
new_list = ['the only element']
print(new_list[1])
# Traceback (most recent call last):
# File "main.py", line 2, in <module>
# print(new_list[1])
# IndexError: list index out of range
The list in the example above contains the only element, but we try to get an element with the index equal to 1. Mind that indexing in lists starts with 0. That's why the IndexError is raised.
This is a very common mistake with lists. Check the indexes you are passing to your list with care and mind the number of values it has in total.
Summary
In conclusion, this topic has provided an overview of several important exceptions in Python. We have discussed how these exceptions can occur in different scenarios, such as working with files, importing modules, handling input, and accessing elements in lists. By understanding the specific causes and subclasses of these exceptions, developers can effectively troubleshoot and debug their code. It is essential to pay attention to details like spelling, correct function and variable names, and proper indexing to avoid these exceptions.