Teacher's Guide
Chapter 7 / Level 6: Customizing Exception Classes
Teaching Objective:
- Student learns how to customize exceptions, including custom pop ups
- Learns the syntax for internal class functions for custom exceptions
- Understands the full range of features available when putting together exceptions
Guide
In this exercise we will be working with customizing custom exception classes, this is the process of adding more properties to the custom exceptions such as arguments and internal functions.
For this level we will be checking the pressure of water tanks and verifying the temperature of water heaters using customized Exceptions. First thing we do is make sure we have two custom exception classes named PressureError and TemperatureError, these will be used to ensure pressure is set to a proper setting and temperature regulated respectively. They both take certain arguments and return a custom error code.
class PressureError(Exception):
def __init__(
self, pressure, message="Water pressure is offset, pressure is: "
, error_code=404):
self.pressure = pressure
self.message = message
self.error_code = error_code
super().__init__(self.message)
async def eval(self):
await player.display(
"[Error: %s {%s %s}]"
% (self.error_code , self.message, self.pressure))
if self.pressure < 100:
return ("Valve has been closed")
if self.pressure > 360:
return ("Valve has been opened")
class TemperatureError(Exception):
def __init__(
self,temp,message="Temperature is not appropiate, enter new temperature: "
, error_code=808):
self.temp = temp
self.message = message
self.error_code = error_code
super().__init__(self.message)
async def eval(self):
await player.display(
"[Error: %s {%s %s}]"
% (self.error_code , self.message, self.temp))
return ("Temperature Corrected")
Walk to the light X marks and face the tanks, use try/except and else code blocks in order to identify any discrepancies in the tanks. If their pressure is too high or too low an error code will pop up and a solution will be applied. There are four (4) variable constants used in this segment: green_pressure , red_pressure , blue_pressure and orange_pressure use them with the PressureError exception code sequence at the corresponding X mark with the matching colored carpet. For example:
try:
if green_pressure < 100 or green_pressure > 360:
raise PressureError(green_pressure)
except PressureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Pressure is appropriate" )
# Do the same with the other 3 variable constants at the correct spots
Once all tanks have been verified or adjusted, walk to the gold X mark and use open() file facing the door. Walk inside the storeroom and face the table, use read() in order to check what temperatures the water heaters are supposed to be set to.
player.open()
await player.read()
Walk to the dark X marks and face the water heaters, use try/except and else code blocks in order to identify any temperature discrepancies in the heaters. If their temperature is too low an error code will pop up and a solution will be applied. There are four (4) variable constants used in this segment: purple_temperature , yellow_temperature , pink_temperature and white_temperature . Use them with the TemperatureError exception code sequence at the corresponding X mark with the matching colored carpet. As an argument, enter the proper temperature the heater should be using the information you previously read in the memo. For example:
try:
if purple_temperature < 100:
raise TemperatureError(120)
except TemperatureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Temperature is ok" )
Do this for all 4 dark X marks over the colored carpets in order to complete the level.
Map overview and path

Code Solution
import player
# Set up Pressure Error Class
class PressureError(Exception):
def __init__(self, pressure, message="Water pressure is offset, pressure is: ", error_code=404):
self.pressure = pressure
self.message = message
self.error_code = error_code
super().__init__(self.message)
async def eval(self):
await player.display("[Error: %s {%s %s}]" % (self.error_code , self.message, self.pressure))
if self.pressure < 100:
return ("Valve has been closed")
if self.pressure > 360:
return ("Valve has been opened")
# Set up Temperature Error Class
class TemperatureError(Exception):
def __init__(self, temp, message="Temperature is not appropiate, enter new temperature: ", error_code=808):
self.temp = temp
self.message = message
self.error_code = error_code
super().__init__(self.message)
async def eval(self):
await player.display("[Error: %s {%s %s}]" % (self.error_code , self.message, self.temp))
return ("Temperature Corrected")
player.turn_left()
player.move_forward()
player.turn_left()
player.move_forward(7)
player.turn_right()
player.move_forward(2)
player.turn_left()
try:
if green_pressure < 100 or green_pressure > 360:
raise PressureError(green_pressure)
except PressureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Pressure is appropriate" )
player.turn_right()
player.move_forward()
player.turn_left()
try:
if red_pressure < 100 or red_pressure > 360:
raise PressureError(red_pressure)
except PressureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Pressure is appropriate" )
player.turn_right()
player.move_forward()
player.turn_left()
try:
if blue_pressure < 100 or blue_pressure > 360:
raise PressureError(blue_pressure)
except PressureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Pressure is appropriate" )
player.turn_right()
player.move_forward()
player.turn_left()
try:
if orange_pressure < 100 or orange_pressure > 360:
raise PressureError(orange_pressure)
except PressureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Pressure is appropriate" )
player.turn_right()
player.move_forward(3)
player.turn_right()
player.open()
player.move_forward(2)
player.turn_left()
await player.read()
player.turn_right()
player.move_forward(3)
player.turn_left()
player.move_forward()
player.turn_left()
try:
if purple_temperature < 100:
raise TemperatureError(120)
except TemperatureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Temperature is ok" )
player.turn_right()
player.move_forward()
player.turn_left()
try:
if yellow_temperature < 100:
raise TemperatureError(140)
except TemperatureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Temperature is ok" )
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward()
player.turn_right()
try:
if pink_temperature < 100:
raise TemperatureError(160)
except TemperatureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Temperature is ok" )
player.turn_left()
player.move_forward()
player.turn_right()
try:
if white_temperature < 100:
raise TemperatureError(120)
except TemperatureError as x:
error = await x.eval()
player.speak(error)
else:
player.speak( "Temperature is ok" )