Advanced Python Development Course
Chapter
>
Level
Exception Handling
Exception Handling
Objective
Pack supplies into boxes and catch any errors that may occur by using Exceptions.
There are several emergency supplies that need to be packed into kits and stored in crates for later use. Doing this is pretty straightforward and can be pulled off using common functions you’ve learned, however sometimes things don’t go as planned. While programming you may have noticed sometimes errors pop up on screen, and red marks show up on the editor where the issue occurred. You can mitigate this by setting up the try and except code blocks.
These code blocks are used to set up exceptions and catch errors before they stop your code. How it work is that you use try on a code that is problematic or could be prone to error and use except to switch the code out for another code in case an error occurs, for example:
#The try code block attempts to run a code try: number + number + string #This code yeilds an error, you can't sum numbers with string except: #If an error occurs the except code block is run instead number + number + number #The code is switched out by a sum of three numbers instead
To start off, grab all materials in the field, those being: water cans , "thread" and three colored blankets: "blue blanket" , "red blanket" , "green blanket" . Begin by walking to the top of the map and grab the three blankets, walk to the light X mark and use the collect() function to procure "thread" form the container. Follow this up by moving to the bottom of the map and grabbing all of the water cans.
Once everything is collected, make your way to the dark X mark over the blue carpet to begin placing the materials you just collected. First add the water and thread into variables with 2 and 3 units respectively, like this: water_can = 2 and thread = 3 . Then set up the kits as variables by using adding up the previously made variables with the colored blankets in the following format:
blue_kit = water_can + thread + "blue blanket" red_kit = water_can + thread + "red blanket" green_kit = water_can + thread + "green blanket" # Add all elements together into kits for ease of storage
Once the kits have been put together in the blue_kit , red_kit, green_kit variables, it’s time to place them into the crates of the corresponding color, however you may have noticed the code is throwing an error. But why? Everything is as it should be and instructions were followed. Sometimes, while intuitive, things won’t work the way we want them, in this case you can’t sum a number with a string in a variable.
So how are we going to remedy this? by using the try / except code blocks to catch the error and convert it to a suitable replacement. For each of the three (3) variables, apply these exceptions, which in the case of an error switches out the code with a numeric value instead of a specific string.
try: blue_kit = water_can + thread + "blue blanket" except: blue_kit = water_can + thread + 1
While in this case we are aware of the error there are some situations where the values inside the variables might not be as straightforward or might be different depending on certain conditions, making this a useful tool for troubleshooting or scooping up runtime errors.
Complete the level by walking to the dark X marks over the colored carpets, facing the crates and using the place() function to set the kit variables of the corresponding color on the correct crates.