Advanced Python Development Course
Chapter
>
Level
Exception Handling
Multiple Exceptions
Objective
Collect and correctly store some medical supplies in crates by using multiple exceptions.
The storehouses in the roof needs to be replenished, some of the supplies were used but never replaced. There is an order put in for more materials in a memo in one of the storerooms, it would be a good idea to get it taken care of. That said there are a variety of different materials that need to be put together and stored in crates.
Because of the different data types and number of materials that are needed to fill crates, we can run into various errors. We can use try / except to catch any possible errors and fix some issues, but what happens when there are multiple possible errors you can encounter in a single operation? To address this we can specify which exceptions we are trying to catch and use multiple exceptions.
To start off, walk to the gold X mark and face the door, use the open() function to enter the storeroom. Once inside walk to the dark X mark and turn to the right in order to use the read() function and check what materials and amounts are being ordered in the memo on the table. Information on list constants are written in the memo, including supplies and their orders for them. Once done, walk to the light X mark inside the storeroom turn to the crate and grab the pads list constant of materials inside using the collect() function, like this: player.collect(pads) .
Inside the storehouse there are stores of "peroxide" and "isopropyl" , walk and collect all of them then make your way outside and head to the dark X mark over a purple carpet. On the purple carpet, while facing the crate, use the collect() function to grab "water bottle" , move on to the dark X mark over the yellow carpet and use the collect() function to grab "glass bottle".
Now that items have been collected, it’s time to fulfil the order and place all the materials inside the storehouse crates on the right. To do this we will first store in the: "water bottle", "peroxide", and "isopropyl" inside a list named supplies. On the dark X marks over the red , green and blue carpets we will be using the crates to store the medical supplies. For this we will set up three lists: blue_crate = [] , green_crate = [] and red_crate = [].
Before placing all the items we must populate all the list with the materials, for this we’re going to use a loop to iterate though the lists and combine them into one single list. This will put all supplies in the list and will divide the number of pads items according to the order in the memo in the storeroom.
for x in range(5): blue_crate.append(supplies[x]) blue_crate.append(pads[x]/order[x])
Despite this you’ll notice that errors will pop up, despite everything being being done correctly and the code being sound. However here’s the issue, the supplies list only has three (3) items, but the loop runs five (5) times causing an Index Error. Meanwhile for the pads list not only does it also have only three (3) items but also one of the order values is 0 , any number divided by 0 causes a Zero Division Error because no number can be divided by 0 in mathematics.
This is where multiple exceptions are useful, we use the try / except code blocks to catch the errors and allow us to proceed with the code. Given there are two errors that occur we must identify each error in order to catch the specific exceptions. This is done by expanding the except code blocks in this fashion: except IndexError: and except ZeroDivisionError:. For example:
for x in range(5): try: blue_crate.append(supplies[x]) blue_crate.append(pads[x]/order[x]) except IndexError: blue_crate.append("glass bottle") except ZeroDivisionError: blue_crate.append(0)
The Index Error is corrected by filling in missing spots in the list with "glass bottle" and the Zero Division Error is addressed by adding a value of 0 if the illegal action happens.
Do this for the blue_crate = [] , green_crate = [] and red_crate = [] lists. Walk to the dark X mark over the blue , green and red carpets and use the place() function to store the lists in the corresponding crates in order to complete the level.