Teacher's Guide
Chapter 7 / Level 2: Multiple Exceptions
Teaching Objective:
- Student learns how to execute multiple except code blocks and their syntax
- Learns specific types of exceptions and how they differ from blanket exceptions
- Understands how to multiple exceptions are useful for troubleshooting specific problems and debugging
Guide
In this exercise we will be working with multiple exceptions, this is the process of identifying and catching different types of errors at the same time. The process of doing this involves using the same try / except as in the previous chapter however this time we add multiple except blocks outlining the error we are trying to catch.
In this level we’ll be collecting several types of medical supplies and placing them in crates for later shipment. First walk to the gold X mark and open() the door to access the storehouse. Once inside, on the dark X mark use read() to check the amounts of items and order from a memo on the table. Walk to the light X mark and grab the pads list constant using the collect() function.
player.collect(pads)
Walk through the storeroom and grab all the bottles of "peroxide", and "isopropyl" then walk out of the storeroom and head for the dark X mark over the purple carpet. On that X mark use collect() to grab "water bottle" then move on to the dark X mark over the yellow carpet and use the collect() to grab "glass bottle" .
player.collect("water bottle")
............
player.collect("glass bottle")
Now that all supplies have been collected, they are to be consolidated into lists and placed into crates at the storehouse. Put the "water bottle", "peroxide" and "isopropyl" materials in a list named supplies. Follow this up by creating three (3) lists, one for each of the crates in the storehouse named: blue_crate = [] , green_crate = [] and red_crate = [] .
supplies = ["water bottle", "peroxide", "isopropyl"]
blue_crate = []
green_crate = []
red_crate = []
Now that all lists are set up it’s time to consolidate materials into the lists. Use a loop to place the supplies list and place the result of the divided lists constants read in the memo. For example:
for x in range(5):
blue_crate.append(supplies[x])
blue_crate.append(pads[x]/order[x])
This will cause errors because there are less than five (5) items in the supplies list and because one of the items in the order list is 0 causing a zero division error which is an illegal mathematical equation. To fix this issue we must apply multiple exceptions using the try / except code blocks. The specific exceptions need to be named and tied to the except block, in this case we use: IndexError and 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)
Do this same formula for blue_crate = [] , green_crate = [] and red_crate = [] then walk to the rest of the dark X marks and use place() to set down the lists in the crates of each corresponding color in order to complete the level.
Map overview and path

Code Solution
import player
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward()
player.open()
player.move_forward(2)
player.turn_right()
player.read()
player.turn_left()
player.move_forward()
player.turn_right()
player.collect(pads)
player.turn_right()
player.turn_right()
player.move_forward(4)
player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward(4)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward()
player.turn_left()
player.collect("water bottle")
player.turn_right()
player.move_forward()
player.turn_left()
player.collect("empty bottle")
supplies = ["water bottle", "peroxide", "isopropyl"]
blue_crate = []
green_crate = []
red_crate = []
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)
for x in range(5):
try:
green_crate.append(supplies[x])
green_crate.append(pads[x]/order[x])
except IndexError:
green_crate.append("glass bottle")
except ZeroDivisionError:
green_crate.append(0)
for x in range(5):
try:
red_crate.append(supplies[x])
red_crate.append(pads[x]/order[x])
except IndexError:
red_crate.append("glass bottle")
except ZeroDivisionError:
red_crate.append(0)
player.turn_left()
player.turn_left()
player.move_forward(4)
player.place(blue_crate)
player.turn_left()
player.move_forward()
player.turn_right()
player.place(green_crate)
player.turn_left()
player.move_forward()
player.turn_right()
player.place(red_crate)