Teacher's Guide

Chapter 7 / Level 4: Wrapping up an Exception

Teaching Objective:

  • Student learns how to use the finally code block and asset keyword
  • Understands how to wrap up exceptions while using all code blocks learned so far
  • Learns how to run the assert keyword and it’s exception error in conjunction in order to troubleshoot

Guide

In this exercise we’ll be working with wrapping up exceptions, particularly the finally code block and the assert keyword. The finally code block is an optional block placed at the end of a try / except sequence, this block is always executed once the sequence ends wrapping it up. The assert keyword is used to scan for errors, by checking if a code is true or false, allowing you to run an exception sequence to resolve any outstanding issues.

The objective of the level is to verify money supply in the accounting office of various containers that are being transported from the office. Start off by walking to the light X mark in front of the door and using open() to access the office. Follow this up by making your way to the gold X mark and using collect() to acquire the categories list constant and the savings variable constant.

player.open()

.................

player.collect(savings)
player.collect(categories)

Walk to the dark X mark over the blue carpet, from here we will be checking the money supply in the sacks on the left side of the room. Here we will be verifying that each sack has the proper items as outlined by the categories list. There are three dictionaries: blue_sack , yellow_sack and red_sack corresponding to each carpet in front of carpets. If all three dictionaries have all the necessary categories they will pass verification, otherwise the necessary elements will be placed() in the sack. There are three supply categories: "exports" , "imports" and "stock" , as such a for loop is run to verify.

For example, on the X mark over the blue carpet, for the blue_sack dictionary.

for x in range(3):
    player.speak(
      "%s = %s" % (categories[x] , blue_sack[categories[x]]))

player.speak("All assets accounted for")

Run the code on the X marks over the: blue, yellow and red carpets with the corresponding dictionary constants. Inevitably errors will pop up as you verify, in particular Dictionary Key Error, which will happen if a category is missing. Use the try / except / finally to identify and place missing items in the sacks. For example:

try:
  for x in range(3):
    player.speak("%s = %s" % (categories[x] , blue_sack[categories[x]]))
except:
  player.speak("Bag is missing %s, adding now" % (categories[x]))
  player.place(categories[x])
finally:
  player.speak("All assets accounted for")

Do the same procedure using yellow_sack and red_sack in the respective colored carpets.

Walk to the X mark over the green carpet, on the row of chests, verify that the chests have a supply of 30 . There are three (3) variable constants corresponding to each of the chests based on the colored carpets in front of them: green_chest , purple_chest , orange_chest . Using the assert keyword verify the supply in conjunction with try / except /finally, if the supply isn’t equal to 30, the missing amount is taken from the savings variable you picked up earlier and added to the chests. Once everything has been verified and corrected the finally code block is used to close() the chests.

try:
  assert(green_chest = 30)
except AssertionError:
  player.speak("Chest is not full %s/30" % green_chest)
  drop = 30-green_chest
  savings -= drop
  player.place(drop)
else:
  player.speak("All assets accounted for")
finally:
  player.close()

Do this process on all three X marks in front of chests and make sure you apply the correct green_chest , purple_chest , orange_chest variable constant in the sequence based on the colored carpets under the X marks. Once done, walk to the light X mark in front of crate and use place() to store the savings variable constant to complete the level.

Map overview and path

Map overview and path

Code Solution

import player

player.move_forward(7)
player.turn_left()
player.move_forward(4)
player.turn_left()
player.open()
player.move_forward(2)
player.turn_right()
player.move_forward(2)

player.collect(savings)
player.collect(categories)

player.turn_left()
player.turn_left()
player.move_forward(4)

try:
  for x in range(3):
    player.speak("%s = %s" % (categories[x] , blue_sack[categories[x]]))
except:
  player.speak("Bag is missing %s, adding now" % (categories[x]))
  player.place(categories[x])
finally:
  player.speak("All assets accounted for")
    
player.turn_right()
player.move_forward()
player.turn_left()

try:
  for x in range(3):
    player.speak("%s = %s" % (categories[x] , yellow_sack[categories[x]]))
except:
  player.speak("Bag is missing %s, adding now" % (categories[x]))
  player.place(categories[x])
finally:
  player.speak("All assets accounted for")

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

try:
  for x in range(3):
    player.speak("%s = %s" % (categories[x] , red_sack[categories[x]]))
except:
  player.speak("Bag is missing %s, adding now" % (categories[x]))
  player.place(categories[x])
finally:
  player.speak("All assets accounted for")

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

try:
  assert(green_chest == 30)
except AssertionError:
  player.speak("Chest is not full %s/30" % green_chest)
  drop = 30-green_chest
  savings -= drop
  player.place(drop)
else:
  player.speak("All assets accounted for")
finally:
  player.close()
  
player.turn_right()
player.move_forward()
player.turn_left()
  
try:
  assert(purple_chest == 30)
except AssertionError:
  player.speak("Chest is not full %s/30" % purple_chest)
  drop = 30-purple_chest
  savings -= drop
  player.place(drop)
else:
  player.speak("All assets accounted for")
finally:
  player.close()

player.turn_right()
player.move_forward()
player.turn_left()
  
try:
  assert(orange_chest == 30)
except AssertionError:
  player.speak("Chest is not full %s/30" % orange_chest)
  drop = 30-orange_chest
  savings -= drop
  player.place(drop)
else:
  player.speak("All assets accounted for")
finally:
  player.close()
  
player.turn_right()
player.move_forward()
player.place(savings)
Chapter 7 / Level 4: Wrapping up an Exception | Teacher's Guide