Debugging and Understanding Errors 🐞
Get prepared for mistakes
- Background
- 1) Understand Error Messages
- 2) Spot the Sources of Errors
- 3) Google and stackoverflow
- 4) Use Try and Except
- 5) Check Documentation
- Additional information
- References:
As mentioned in the instructions, all materials can be open in Colab
as Jupyter notebooks. In this way users can run the code in the cloud. It is highly recommanded to follow the tutorials in the right order.
Background
A bug occurs when things do not work the way you want it to, even though Python only give what you ask for. It is because we have a different understandings compared to the programming languages. To resolve this, we need to find out the sources of errors and adjust our code accordingly. In this notebook you will learn different approach to resolve potential issues and some tips to avoid them from happening.
Presumption:
https://www.w3schools.com/python/python_try_except.asp
https://www.tutorialsteacher.com/python/error-types-in-python
1) Understand Error Messages
As you have seen from the link above, there are many different types of errors in Python which is meant to be helpful to tell user "what is wrong?". But some errors might be more common than the others. Let's look at some common errors.
Let's say you want to add a second collection to the first one (collection). But instead of "collection", you wrote "Collection".
In this case Python tells you
NameError: name 'Collection' is not defined
It is because Python is searching for "Collection" and cannot find one.
Name Error: Raised when a variable is not found in the local or global scope.
What you need to do it just correct the name.
collection = ["Chinese Posters in Harvard-Yenching Manchukuo Collection"]
Collection + ["Linked Archive of Asian Postcards"]
Error occurs too when the library is not imported before use.
np.max([1,2])
Or when you have one item in list while you are asking for the second one (Remember Python starts with 0). You get:
IndexError: list index out of range
Index Error: Raised when the index of a sequence is out of range.
Then you need to correct the index.
collection = ["Chinese Posters in Harvard-Yenching Manchukuo Collection"]
collection[1]
There is also Type Error: Raised when a function or operation is applied to an object of an incorrect type.
It occurs because np.max() looks for a number and we input a string.
import numpy as np
np.max([3]) # this works
import numpy as np
np.max([collection])
Another error that is really common is that when you type something grammatically wrong in the Python sense.
Syntax Error: Raised by the parser when a syntax error is encountered.
collection = ["Chinese Posters in Harvard-Yenching Manchukuo Collection"]
collection[0) # it should be [0], not[0)
Another way to spot errors in code is that try to reduce the code you have. For example:
Let's say you want to find out the length of the first advertisement in the list (商務印書館發行書目介紹), and you built a function for it. But instead of 11 characters, you get 2.
Although there is no problems running this code, as it is not giving you what you want, it is also a bug.
What you can do is to reduce the code and to see if things work the way you want.
ad = ["商務印書館發行書目介紹","女界寶、非洲樹皮丸、助肺呼吸香膠、家普魚肝油、清血解毒海波藥、納佛補天汁、良丹(五洲大藥房)"]
import re # this you will learn in the notebook web scrapping so you do not need to understand everything for now
def number_of_character(text):
list_ = list(text) # list() is used to split words into a list of characters
length = len(list_) # len() is to check the length
return length
number_of_character(ad)
Let's check the first step: Can we use list to split the characters as we want?
list(ad)
And we realize, we want to split 商務印書館發行書目介紹, but this is not done!
list("advertisement") # this is what we want, to split characters
And then we might find out, it only work if we use ad[0] instead.
list(ad[0])
ad = ["商務印書館發行書目介紹","女界寶、非洲樹皮丸、助肺呼吸香膠、家普魚肝油、清血解毒海波藥、納佛補天汁、良丹(五洲大藥房)"]
import re # this you will learn in the notebook web scrapping so you do not need to understand everything for now
def number_of_character(text):
list_ = list(text)
length = len(list_)
return length
number_of_character(ad[0])
Sometimes errors are not so clear to you. What you can do is to copy the errors and Google them. Stackoverflow is also another website that can be really helpful.
collection = ["Chinese Posters in Harvard-Yenching Manchukuo Collection", "Linked Archive of Asian Postcards"]
typ = [1,0]
collection[typ]
Then you might find out what you need to select collection item from typ is to use Numpy array instead of list.
collection = np.array(["Chinese Posters in Harvard-Yenching Manchukuo Collection", "Linked Archive of Asian Postcards"])
typ = np.array([1,0])
collection[typ]
4) Use Try and Except
In order to avoid errors, what we can also do is to use try and except. It means asking Python to try something out, if it does not work, then do Plan B instead of giving you errors.
Be careful that this method only appy if the errors come from unexpected outliners in inputs. It will not give you any meaningful results if the errors lie in your code itself.
It works as follow:
try:
(do plan a)
# <- indented block
except:
(do plan b)
# <- indented block
try:
print(1+1)
except:
print(0)
For example, the same function work well for the first and second item, but there is a typo in the third item, so that it is not a string, but an integer.
In this case, the function will not work as it expects a string.
collection = ["Chinese Posters in Harvard-Yenching Manchukuo Collection", "Linked Archive of Asian Postcards", 4]
def number_of_character(text):
list_ = list(text)
length = len(list_)
return length
number_of_character(collection[2])
What we can do is to set up a Plan B:
If the words cannot be split, then use an empty list ([]).
OUR PLAN B
except:
list_ = []
def number_of_character(text):
try:
list_ = list(text)
except:
list_ = []
length = len(list_)
return length
number_of_character(collection[2])
Now, we do not get the same error anymore. It is particularly useful when we are automating the task: because if we want going through thousands of documents, we do not want the code to crash because of one little typo.
5) Check Documentation
If the errors come from your code itself, the easiest way to inspect the problems is to check the documentation of the function you used. If you are using Jupyter Notebook, you can always highlight the function you typed and the documentation of this function will appear, illustrating what inputs are expected.
You can also choose to directly Google the function and check the examples. For exmaple, if the following error occurs, by searching the reverse function "python reverse()" we can see from the documentation that the list is expected before the function ad(), not inside the ().
reverse(ad)
ad
ad.reverse()
ad
Coding Practice Basics
Previous Lesson:Functions and Loops Basics
Next Lesson:Additional information
This notebook is provided for educational purpose and feel free to report any issue on GitHub.
Author: Ka Hei, Chow
License: The code in this notebook is licensed under the Creative Commons by Attribution 4.0 license.
Last modified: December 2021
References:
https://www.tutorialsteacher.com/python/error-types-in-python