Teacher's Guide

Chapter 7 / Level 5: Custom Exceptions

Teaching Objective:

  • Student learns how to create and manipulate custom exceptions
  • Learns the syntax and breakdown of how custom exceptions are executed and implemented
  • Understands how flexible and powerful creating your own exceptions can be and how useful they are for troubleshooting

Guide

In this exercise we will be working with custom exceptions, these are exceptions that you create yourself and can use instead or the built in exceptions we’ve been using so far. This is achieved by using classes to declare their usage in the code you’re working on.

The objective of this level is to collect messages and send them to the west sector of the farm via carrier pigeon. Set up classes named InvalidLocation and UnsuitableSelection in order to filter out specific strings and numbers respectively.

class InvalidLocation(Exception):
    "Raised when location is invalid"
    pass

class UnsuitableSelection(Exception):
    "Raised when pidgeon is 2 or younger"
    pass

Set up a list named prep in order to add all that’s needed to send out the messages via carrier pigeon.

prep = []

Walk to the X mark over the red carpet and use the try/except and else sequence in conjunction with InvalidLocation in order to identify memos that are bound for the "west" sector of the farm. Any memos that are not bound to that sector will trigger the except clause using the InvalidLocation custom exception. If there is no exception, the content of the variable is appended to the prep list. For example:

try:
  if red_memo != "west":
    raise InvalidLocation

except InvalidLocation:
  player.speak(
    "This is not an order that needs to be sent out")
else:
  player.collect(red_memo)
  prep.append(red_memo)

There are four variable constants that are used in this section: red_memo , blue_memo, green_memo and orange_memo . They correspond to the each X mark over the matching colored carpet, use the sequence in the example above with the correct variable constant while facing the tables.

Once done, walk to the gold X mark in front of the door, use open() and enter the room and turn around at the light X mark and close() the door.

player.open()

..........

player.close()

Walk to the X mark over the purple carpet and use the try/except and else sequence in conjunction with UnsuitableSelection in order to check if the pigeon in the pen is seasoned enough to make a delivery or if it’s too young to make the flight. Any pigeons that are too young will trigger the except clause using the UnsuitableSelection custom exception. If there is no exception, the content of the variable is appended to the prep list. For example:

try:
  if purple_pen <= 2:
    raise UnsuitableSelection

except UnsuitableSelection:
  player.speak("Pigeon unsuitable to deliver")
else:
  prep.append(purple_pen)
  player.speak("Pigeon Collected"

There are four more variable constants that are used in this section: purple_pen, yellow_pen, white_pen and pink_pen. They correspond to the each X mark over the matching colored carpet, use the sequence in the example above with the correct variable constant while facing the pigeon pens.

After all memos and pigeons have been collected, walk to the gold X mark and place() the prep list with the birds equipped with the messages in order to complete the level.

player.place(prep)

Map overview and path

Map overview and path

Code Solution

import player

class InvalidLocation(Exception):
    "Raised when location is invalid"
    pass

class UnsuitableSelection(Exception):
    "Raised when pidgeon is 2 or younger"
    pass

prep = []
    
player.turn_left()
player.turn_left()
player.move_forward(6)
player.turn_right()
player.move_forward(3)
player.turn_left()

try:
  if red_memo != "west":
    raise InvalidLocation
        
except InvalidLocation:
  player.speak(
    "This is not an order that needs to be sent out")
else:
  player.collect(red_memo)
  prep.append(red_memo)
  
player.turn_right()
player.move_forward()
player.turn_left()

try:
  if blue_memo != "west":
    raise InvalidLocation
        
except InvalidLocation:
  player.speak(
    "This is not an order that needs to be sent out")
else:
  player.collect(blue_memo)
  prep.append(blue_memo)
  
player.turn_right()
player.move_forward(2)
player.turn_left()

try:
  if green_memo != "west":
    raise InvalidLocation
        
except InvalidLocation:
  player.speak(
    "This is not an order that needs to be sent out")
else:
  player.collect(green_memo)
  prep.append(green_memo)

player.turn_right()
player.move_forward()
player.turn_left()

try:
  if orange_memo != "west":
    raise InvalidLocation
        
except InvalidLocation:
  player.speak(
    "This is not an order that needs to be sent out")
else:
  player.collect(orange_memo)
  prep.append(orange_memo)

player.turn_right()
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_left()

player.open()
player.move_forward(2)
player.turn_left()
player.turn_left()
player.close()

player.turn_left()
player.move_forward(2)
player.turn_right()

try:
  if purple_pen <= 2:
    raise UnsuitableSelection
        
except UnsuitableSelection:
  player.speak("Pigeon unsuitable to deliver")
else:
  prep.append(purple_pen)
  player.speak("Pigeon Collected")
  
player.turn_right()
player.move_forward()
player.turn_left()

try:
  if yellow_pen <= 2:
    raise UnsuitableSelection
        
except UnsuitableSelection:
  player.speak("Pigeon unsuitable to deliver")
else:
  prep.append(yellow_pen)
  player.speak("Pigeon Collected")
  
player.turn_right()
player.move_forward(2)
player.turn_left()

try:
  if white_pen <= 2:
    raise UnsuitableSelection
        
except UnsuitableSelection:
  player.speak("Pigeon unsuitable to deliver")
else:
  prep.append(white_pen)
  player.speak("Pigeon Collected")
  
player.turn_right()
player.move_forward()
player.turn_left()

try:
  if pink_pen <= 2:
    raise UnsuitableSelection
        
except UnsuitableSelection:
  player.speak("Pigeon unsuitable to deliver")
else:
  prep.append(pink_pen)
  player.speak("Pigeon Collected")
  
player.turn_right()
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward()
player.place(prep)
Chapter 7 / Level 5: Custom Exceptions | Teacher's Guide